public void GetPropertyGetter_AnonymousObjectProperty_ReturnsPropertyValue()
        {
            var input = new
            {
                A     = "aye",
                B     = "bee",
                C     = "sea",
                One   = 1,
                Two   = 2,
                Three = 3,
                Do    = "doe",
                Re    = "ray",
                Mi    = "me"
            };

            var propertyInfo = input.GetType().GetProperty("Two");

            Assert.NotNull(propertyInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);

            Assert.NotNull(getter);

            Assert.Equal(2, getter(input));
        }
        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 GetFieldSetter_StaticReadonlyField_ReturnsNull()
        {
            var fieldInfo = typeof(Guid).GetField("Empty");

            Assert.NotNull(fieldInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            Assert.Null(setter);
        }
        public void GetFieldSetter_ConstField_ReturnsNull()
        {
            var fieldInfo = typeof(Int32).GetField("MaxValue");

            Assert.NotNull(fieldInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            Assert.Null(setter);
        }
        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_NullCtorInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                FactoryDelegate factory = DynamicMethodGenerator.GetTypeFactory((ConstructorInfo)null);
            });

            Assert.Equal("ctor", ex.ParamName);
        }
        public void GetFieldSetter_NullInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(null);
            });

            Assert.Equal("fieldInfo", ex.ParamName);
        }
        public void GetPropertyGetter_NullInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(null);
            });

            Assert.Equal("propertyInfo", ex.ParamName);
        }
        public void GetMethodProxy_NullInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                ProxyDelegate setter = DynamicMethodGenerator.GetMethodProxy(null);
            });

            Assert.Equal("methodInfo", ex.ParamName);
        }
        public void GetMethodProxy_MethodNoArgsOneReturn_BuildsProxyAndInvokes()
        {
            var input = new Example();

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("GetMi"));

            Assert.NotNull(proxy);
            var actual = (string)proxy(input);

            Assert.Equal("me", actual);
        }
        public void GetFieldGetter_StaticField_ReturnsFieldValue()
        {
            var fieldInfo = typeof(Example).GetField("MyStatic");

            Assert.NotNull(fieldInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(fieldInfo);

            Assert.NotNull(getter);

            Assert.Equal(42, getter(null));
        }
        public void GetFieldGetter_StaticReadonlyField_ReturnsFieldValue()
        {
            var fieldInfo = typeof(Guid).GetField("Empty");

            Assert.NotNull(fieldInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(fieldInfo);

            Assert.NotNull(getter);

            Assert.Equal(Guid.Empty, getter(null));
        }
        public void GetPropertySetter_GetterOnlyProperty_ReturnsPropertyValue()
        {
            var input = new Example();

            var propertyInfo = input.GetType().GetProperty("Three_Getter");

            Assert.NotNull(propertyInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo);

            Assert.Null(setter);
        }
        public void GetMethodProxy_ArgsTypeMismatchWhenCalling_ThrowsArgumentNullException()
        {
            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Reset"));

            Assert.NotNull(proxy);

            InvalidCastException ex = Assert.Throws <InvalidCastException>(
                delegate()
            {
                proxy(new Example(), 1, 2, 3, 4, 5, 6, 7, 8, 9);
            });
        }
        public void GetMethodProxy_ArgsMissingWhenCalling_ThrowsArgumentNullException()
        {
            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Reset"));

            Assert.NotNull(proxy);

            ArgumentException ex = Assert.Throws <ArgumentException>(
                delegate()
            {
                var actual = proxy(new Example(), "alpha", "bravo", "charlie", -1, -2, -3);
            });
        }
        public void GetMethodProxy_MethodOneArgOneReturn_BuildsProxyAndInvokes()
        {
            var input = new Example();

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Swap"));

            Assert.NotNull(proxy);
            var actual = (string)proxy(input, "foo");

            Assert.Equal("aye", actual);
            Assert.Equal("foo", input.A);
        }
        public void GetPropertySetter_1MillionPropertySets_PerformsInAround10ms()
        {
            Example      instance     = new Example();
            PropertyInfo propertyInfo = typeof(Example).GetProperty("A", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo);

            for (long i = 0; i < MaxCount; i++)
            {
                setter(instance, "alpha");
            }
        }
        public void GetFieldGetter_ConstField_ReturnsFieldValue()
        {
            var fieldInfo = typeof(Int32).GetField("MaxValue");

            Assert.NotNull(fieldInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(fieldInfo);

            Assert.NotNull(getter);

            Assert.Equal(Int32.MaxValue, getter(null));
        }
        public void GetPropertyGetter_PrivateGetterProperty_ReturnsPropertyValue()
        {
            var input = new Example();

            var propertyInfo = input.GetType().GetProperty("Mi");

            Assert.NotNull(propertyInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);

            Assert.NotNull(getter);
            Assert.Equal("me", getter(input));
        }
Пример #21
0
        /// <summary>
        /// Creates a field setter delegate for the specified property or field
        /// </summary>
        /// <param name="memberInfo">PropertyInfo or FieldInfo</param>
        /// <returns>SetterDelegate for property or field, null otherwise</returns>
        public static SetterDelegate GetSetter(MemberInfo memberInfo)
        {
            if (memberInfo is PropertyInfo)
            {
                return(DynamicMethodGenerator.GetPropertySetter((PropertyInfo)memberInfo));
            }

            if (memberInfo is FieldInfo)
            {
                return(DynamicMethodGenerator.GetFieldSetter((FieldInfo)memberInfo));
            }

            return(null);
        }
        public void GetMethodProxy_1MillionMethodCalls_PerformsInAround50ms()
        {
            Example    instance   = new Example();
            MethodInfo methodInfo = typeof(Example).GetMethod("GetMi", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(methodInfo);

            string value = null;

            for (long i = 0; i < MaxCount; i++)
            {
                value = (string)proxy(instance);
            }
        }
        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);
            });
        }
        public void GetFieldGetter_1MillionFieldGets_PerformsInAround10ms()
        {
            Example   instance  = new Example();
            FieldInfo fieldInfo = typeof(Example).GetField("a", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(fieldInfo);

            string value = null;

            for (long i = 0; i < MaxCount; i++)
            {
                value = (string)getter(instance);
            }
        }
        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 GetPropertyGetter_PrivateProperty_ReturnsPropertyValue()
        {
            var input = new Example();

            var propertyInfo = input.GetType().GetProperty("Two", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

            Assert.NotNull(propertyInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);

            Assert.NotNull(getter);

            Assert.Equal(2, getter(input));
        }
        public void GetFieldGetter_PrivateField_ReturnsFieldValue()
        {
            var input = new Example();

            var fieldInfo = input.GetType().GetField("two", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

            Assert.NotNull(fieldInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(fieldInfo);

            Assert.NotNull(getter);

            Assert.Equal(2, getter(input));
        }
        public void GetFieldGetter_ValueTypeField_ReturnsFieldValue()
        {
            var input = new Example();

            var fieldInfo = input.GetType().GetField("one");

            Assert.NotNull(fieldInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(fieldInfo);

            Assert.NotNull(getter);

            Assert.Equal(1, getter(input));
        }
        public void GetPropertyGetter_StaticProperty_ChangesFieldValue()
        {
            var propertyInfo = typeof(Example).GetProperty("Solo");

            Assert.NotNull(propertyInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);

            Assert.NotNull(getter);

            var actual = getter(null);

            Assert.Equal("Single", Example.Solo);
        }
Пример #30
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));
        }