Esempio n. 1
0
        public void PassThroughConstructors()
        {
            {
                TypeBuilder b = CreateTypeBuilder(typeof(BaseOne));
                b.DefinePassThroughConstructors(c => c.Attributes | MethodAttributes.Public);
                Type    t    = b.CreateTypeInfo().AsType();
                BaseOne one1 = (BaseOne)Activator.CreateInstance(t, 5);
                Assert.That(one1.CtorMessage, Is.EqualTo(".private.protected"));
                BaseOne one2 = (BaseOne)Activator.CreateInstance(t, "a string");
                Assert.That(one2.CtorMessage, Is.EqualTo(".private.public"));
            }
            {
                TypeBuilder b = CreateTypeBuilder(typeof(BaseTwo));
                b.DefinePassThroughConstructors(c => c.Attributes | MethodAttributes.Public);
                Type    t    = b.CreateTypeInfo().AsType();
                var     ctor = t.GetConstructors()[0];
                BaseTwo two  = (BaseTwo)ctor.Invoke(new object[] { new int[] { 1, 2, 3, 4 } });
                Assert.That(two.CtorMessage, Is.EqualTo("4"));
            }
            {
                TypeBuilder b = CreateTypeBuilder(typeof(BaseThree));
                b.DefinePassThroughConstructors(
                    c => c.Attributes | MethodAttributes.Public,
                    (ctor, attrData) => attrData.NamedArguments.Any(),
                    (param, attrData) => attrData.ConstructorArguments.Any());

                Type      t       = b.CreateTypeInfo().AsType();
                var       theCtor = t.GetConstructors()[0];
                BaseThree three   = (BaseThree)theCtor.Invoke(new object[] { "s0", "s1", "s2" });
                Assert.That(three.CtorMessage, Is.EqualTo("s0s1s2"));
                // Only attribute defined via Named arguments are defined on the final ctor.
                CollectionAssert.AreEquivalent(new string[] { "OnCtorByProperty", "OnCtorByField" }, theCtor.GetCustomAttributes <CustAttr>().Select(a => a.Name ?? a.FieldName));
                // Only the one defined by constructor argument is redefined.
                Assert.That(theCtor.GetParameters()[0].GetCustomAttributes <CustAttr>().Single().Name, Is.EqualTo("OnParamByParam"));
                Assert.That(theCtor.GetParameters()[1].GetCustomAttributes <CustAttr>(), Is.Empty);
                Assert.That(theCtor.GetParameters()[2].GetCustomAttributes <CustAttr>(), Is.Empty);
            }
            {
                TypeBuilder b = CreateTypeBuilder(typeof(BaseThree));
                b.DefinePassThroughConstructors(c => c.Attributes | MethodAttributes.Public);
                Type      t     = b.CreateTypeInfo().AsType();
                var       ctor  = t.GetConstructors()[0];
                BaseThree three = (BaseThree)ctor.Invoke(new object[] { "s0", "s1", "s2" });
                Assert.That(three.CtorMessage, Is.EqualTo("s0s1s2"));
                // Everything is defined.
                CollectionAssert.AreEquivalent(new string[] { "OnCtorByParam", "OnCtorByProperty", "OnCtorByField" }, ctor.GetCustomAttributes <CustAttr>().Select(a => a.Name ?? a.FieldName));
                Assert.That(ctor.GetParameters()[0].GetCustomAttributes <CustAttr>().Single().Name, Is.EqualTo("OnParamByParam"));
                Assert.That(ctor.GetParameters()[1].GetCustomAttributes <CustAttr>().Single().Name, Is.EqualTo("OnParamByProperty"));
                Assert.That(ctor.GetParameters()[2].GetCustomAttributes <CustAttr>().Single().FieldName, Is.EqualTo("OnParamByField"));
            }
        }
Esempio n. 2
0
 public WithDependencies(BaseOne baseOne, BaseTwo baseTwo, BaseThree baseThree)
 {
     BaseOne   = baseOne;
     BaseTwo   = baseTwo;
     BaseThree = baseThree;
 }