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

            Assert.Equal("fieldInfo", ex.ParamName);
        }
        public void GetFieldSetter_ConstField_ReturnsNull()
        {
            var fieldInfo = typeof(Int32).GetField("MaxValue");

            Assert.NotNull(fieldInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            Assert.Null(setter);
        }
        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_1MillionFieldSets_PerformsInAround10ms()
        {
            Example   instance  = new Example();
            FieldInfo fieldInfo = typeof(Example).GetField("a", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            for (long i = 0; i < MaxCount; i++)
            {
                setter(instance, "alpha");
            }
        }
Ejemplo n.º 5
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 GetFieldSetter_PrivateField_ReturnsFieldValue()
        {
            var input    = new Example();
            var expected = -2;

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

            Assert.NotNull(fieldInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            Assert.NotNull(setter);
            setter(input, expected);

            Assert.Equal(expected, fieldInfo.GetValue(input));
        }
        public void GetFieldSetter_ValueTypeField_ReturnsFieldValue()
        {
            var input    = new Example();
            var expected = -1;

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

            Assert.NotNull(fieldInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            Assert.NotNull(setter);
            setter(input, expected);

            Assert.Equal(expected, input.One);
        }
        public void GetFieldSetter_StaticField_ChangesFieldValue()
        {
            var fieldInfo = typeof(Example).GetField("MyStatic");

            Assert.NotNull(fieldInfo);

            SetterDelegate setter = DynamicMethodGenerator.GetFieldSetter(fieldInfo);

            Assert.NotNull(setter);

            var prev = Example.MyStatic;

            try
            {
                setter(null, 2010);
                Assert.Equal(2010, Example.MyStatic);
            }
            finally
            {
                Example.MyStatic = prev;
            }
        }