Skip to content

Invalid Parameter default in derived Method #291

Open
@ENikS

Description

@ENikS

Invalid default value in overridden method parameters

Starting with Unity v5, parameters with default values are initialized with that value in case import can not be resolved.

Values in derived types

Consider the following example:

        public class TestType
        {
            [InjectionMethod]
            public virtual void Method(int value = 11) => Value = value;


            public virtual int Expected => 11;
            public virtual object Value { get; protected set; }
        }

        public class TestTypeDerived : TestType
        {
            public override void Method(int value = 22) => Value = value;

            public override int Expected => 22;
        }

        [TestMethod]
        public void TestCase()
        {
            var original = Container.Resolve<TestType>();
            var derived = Container.Resolve<TestTypeDerived>();

            // Validate
            Assert.IsNotNull(original);
            Assert.AreEqual(original.Expected, original.Value);

            Assert.IsNotNull(derived);
            Assert.AreEqual(derived.Expected, derived.Value);  <-- fails 
        }

TestType defines virtual method marked for injection. By default int is not registered, so Unity uses default value to invoke the method.
In derived type TestTypeDerived the method is overridden with different default value, but when resolved, default value from parent type is used.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions