Exemplo n.º 1
0
        public void ConstructValidTests()
        {
            var constructor = new ObjectConstructor();
            var testarg     = new TestArg()
            {
                Text = "amagad"
            };
            var testarg2 = new TestArg2()
            {
                Text = "amagad"
            };

            //anyone can construct on object!
            constructor.Construct(typeof(TestClass1), testarg);
            constructor.Construct(typeof(TestClass1), testarg2);

            //basic case - identical Type parameter
            var instance = (TestClass2)constructor.Construct(typeof(TestClass2), testarg);

            Assert.AreEqual(instance.A.Text, "amagad");
            //also allowed because testarg2 is a testarg derrived class
            constructor.Construct(typeof(TestClass2), testarg2);

            //not allowed because class 3 explicitly requires a TestArg2
            Assert.Throws <ObjectLacksCompatibleConstructorException>(() => constructor.Construct(typeof(TestClass3), testarg));

            //allowed
            constructor.Construct(typeof(TestClass3), testarg2);

            //valid because even though both constructors are valid there is one that matches EXACTLY on Type
            constructor.Construct(typeof(TestClass4), testarg2);

            var testarg3 = new TestArg3();

            //not valid because there are 2 constructors that are both base classes of TestArg3 so ObjectConstructor doesn't know which to invoke
            var ex = Assert.Throws <ObjectLacksCompatibleConstructorException>(() => constructor.Construct(typeof(TestClass4), testarg3));

            Assert.IsTrue(ex.Message.Contains("Could not pick the correct constructor between"));

            //exactly the same as the above case but one constructor has been decorated with ImportingConstructor
            constructor.Construct(typeof(TestClass5), testarg3);
        }
Exemplo n.º 2
0
 public TestClass3(TestArg2 a)
 {
     A = a;
 }