zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Dynamic Function with parameters #429

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
There seems to be an issue when obtaining a dependent object dynamically using 
a function and passing parameters of the same type.  The following example will 
reproduce the issue that I am seeing.  This unit-test will fail.  If the 
constructor of CHandler was (int, string) and the code was changed accordingly, 
it works.  The issue appears to be when the constructor has parameters of the 
same type.  When the function is invoked to create the dependent object, the 
first passed parameter is passed to all parameters of the same type.  Below is 
an example.  I would think Y should be 200 and not 100.

        [TestMethod]
        public void DynamicInstantiationIssue()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<CHandler>();
            builder.RegisterType<DependantComponent4>();

            var container = builder.Build();
            using (ILifetimeScope scope = container.BeginLifetimeScope())
            {
                var comp = scope.Resolve<DependantComponent4>();
                var dependObj = comp.CreateDependancy(100, 200);
                Assert.AreEqual(100, dependObj.X);
                Assert.AreEqual(200, dependObj.Y);
            }
        }

        public class CHandler
        {
            public int X { get; private set; }
            public int Y { get; private set; }

            public CHandler(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        public class DependantComponent4
        {
            private readonly Func<int, int, CHandler> _func;

            public DependantComponent4(Func<int, int, CHandler> func)
            {
                _func = func;
            }

            public CHandler CreateDependancy(int a, int b)
            {
                return _func(a, b);
            }
        }

Original issue reported on code.google.com by blgrec...@gmail.com on 4 May 2013 at 3:36

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Was able to correct the issue.  Based on the other defect that posted the 
correction but din't include a code example.  Below is the code to correct the 
above issue:

public delegate CHandler CFunc(int x, int y);

    public class DependantComponent4
    {
        private readonly CFunc _func;

        public DependantComponent4(CFunc func)
        {
            _func = func;
        }

        public CHandler CreateDependancy(int a, int b)
        {
            return _func(a, b);
        }
    }

Original comment by blgrec...@gmail.com on 4 May 2013 at 4:25

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 14 May 2013 at 1:00