Exemplo n.º 1
0
        public void FastReflectionInstantiate()
        {
            var fastReflection = new FastReflection();

            var instance1 = fastReflection.Instantiate(typeof(StringBuilder));

            Assert.IsInstanceOfType(instance1, typeof(StringBuilder));

            var instance3 = (string)fastReflection.Instantiate(typeof(string));

            Assert.IsInstanceOfType(instance3, typeof(string));

            var instance4 = (bool)fastReflection.Instantiate(typeof(bool));

            Assert.IsInstanceOfType(instance4, typeof(bool));

            var instance2 = (MockValue)fastReflection.Instantiate(typeof(MockValue));

            Assert.AreEqual(1, instance2.Foo);

            instance2 = (MockValue)fastReflection.Instantiate(typeof(MockValue), 123);
            instance2 = (MockValue)fastReflection.Instantiate(typeof(MockValue), 456);
            instance2 = (MockValue)fastReflection.Instantiate(typeof(MockValue), 789);
            Assert.AreEqual(789, instance2.Foo);

            instance2 = (MockValue)fastReflection.Instantiate(typeof(MockValue), "100");
            Assert.AreEqual(1000, instance2.Foo);

            try
            {
                fastReflection.Instantiate(typeof(MockValue), 1, 2, 3);
                Assert.Fail();
            }
            catch (TypeLoadException)
            {
            }
            catch
            {
                Assert.Fail();
            }

            try
            {
                fastReflection.Instantiate(typeof(MockValue), true);
                Assert.Fail();
            }
            catch (TypeLoadException)
            {
            }
            catch
            {
                Assert.Fail();
            }
        }