public void GetTypeFactory_ArgsMismatchInBuilding_ReturnsNull()
        {
            FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory(
                typeof(Example),
                typeof(string), typeof(string), typeof(string));

            Assert.Null(factory);
        }
        public void GetTypeFactory_1MillionInstantiationsAlt_PerformsInAround50ms()
        {
            FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory(typeof(Example), Type.EmptyTypes);

            for (long i = 0; i < MaxCount; i++)
            {
                Example instance = (Example)factory();
            }
        }
        public void GetTypeFactory_NullCtorInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory((ConstructorInfo)null);
            });

            Assert.Equal("ctor", ex.ParamName);
        }
        public void GetTypeFactory_NullTypeInputAlt_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory((Type)null, Type.EmptyTypes);
            });

            Assert.Equal("type", ex.ParamName);
        }
        public void GetTypeFactory_ArgsTypeMismatchWhenCalling_ThrowsArgumentNullException()
        {
            FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory(
                typeof(Example),
                typeof(string), typeof(string), typeof(string), typeof(int), typeof(int), typeof(int), typeof(string), typeof(string), typeof(string));

            Assert.NotNull(factory);

            InvalidCastException ex = Assert.Throws <InvalidCastException>(
                delegate()
            {
                var actual = (Example)factory(1, 2, 3, 4, 5, 6, 7, 8, 9);
            });
        }
        public void GetTypeFactory_ArgsMissingWhenCalling_ThrowsArgumentNullException()
        {
            FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory(
                typeof(Example),
                typeof(string), typeof(string), typeof(string), typeof(int), typeof(int), typeof(int), typeof(string), typeof(string), typeof(string));

            Assert.NotNull(factory);

            TypeLoadException ex = Assert.Throws <TypeLoadException>(
                delegate()
            {
                var actual = (Example)factory("alpha", "bravo", "charlie", -1, -2, -3);
            });
        }
Exemple #7
0
        /// <summary>
        /// Creates a constructor delegate accepting specified arguments
        /// </summary>
        /// <param name="type">type to be created</param>
        /// <param name="args">constructor arguments type list</param>
        /// <returns>FactoryDelegate or null if constructor not found</returns>
        /// <remarks>
        /// Note: use with caution this method will expose private and protected constructors without safety checks.
        /// </remarks>
        public static FactoryDelegate GetTypeFactory(Type type, params Type[] args)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy, null, args, null);

            if (ctor == null)
            {
                return(null);
            }

            return(DynamicMethodGenerator.GetTypeFactory(ctor));
        }
        public void GetTypeFactory_CtorNoArgsAlt_ReturnsCorrectlyInstantiatedObject()
        {
            var expected = new Example();

            FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory(typeof(Example), Type.EmptyTypes);

            Assert.NotNull(factory);
            var actual = (Example)factory();

            var getters =
                from m in typeof(Example).GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
                let g = DynamicMethodGenerator.GetGetter(m)
                        where (g != null)
                        select g;

            foreach (GetterDelegate getter in getters)
            {
                // assert all of the fields and properties are equal
                Assert.Equal(getter(expected), getter(actual));
            }
        }
        /// <summary>
        /// Creates a default constructor delegate
        /// </summary>
        /// <param name="type">type to be created</param>
        /// <returns>FactoryDelegate or null if default constructor not found</returns>
        /// <remarks>
        /// Note: use with caution this method will expose private and protected constructors without safety checks.
        /// </remarks>
        public static FactoryDelegate GetTypeFactory(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy, null,
#if NETCF
                                                       new Type[] {},
#else
                                                       Type.EmptyTypes,
#endif
                                                       null);

            if (ctor == null)
            {
                return(null);
            }

            return(DynamicMethodGenerator.GetTypeFactory(ctor));
        }
        public void GetTypeFactory_CtorExtraArgs_IgnoresAndReturnsCorrectlyInstantiatedObject()
        {
            var expected = new Example("alpha", "bravo", "charlie", -1, -2, -3, "deer", "sun", "myself");

            FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory(
                typeof(Example),
                typeof(string), typeof(string), typeof(string), typeof(int), typeof(int), typeof(int), typeof(string), typeof(string), typeof(string));

            Assert.NotNull(factory);
            var actual = (Example)factory("alpha", "bravo", "charlie", -1, -2, -3, "deer", "sun", "myself", 4, 5, 6, "extra", false);

            var getters =
                from m in typeof(Example).GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
                let g = DynamicMethodGenerator.GetGetter(m)
                        where (g != null)
                        select g;

            foreach (GetterDelegate getter in getters)
            {
                // assert all of the fields and properties are equal
                Assert.Equal(getter(expected), getter(actual));
            }
        }