Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Parameter default in derived Method #291

Open
ENikS opened this issue Nov 19, 2020 · 0 comments
Open

Invalid Parameter default in derived Method #291

ENikS opened this issue Nov 19, 2020 · 0 comments
Assignees
Labels
Milestone

Comments

@ENikS
Copy link
Contributor

ENikS commented Nov 19, 2020

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.

@ENikS ENikS added the Bug 🐛 label Nov 19, 2020
@ENikS ENikS added this to the 6.0.0 milestone Nov 19, 2020
@ENikS ENikS self-assigned this Nov 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant