public void GetFieldGetter_NullInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                GetterDelegate getter = DynamicMethodGenerator.GetFieldGetter(null);
            });

            Assert.Equal("fieldInfo", ex.ParamName);
        }
        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 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 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));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a field getter delegate for the specified property or field
        /// </summary>
        /// <param name="memberInfo">PropertyInfo or FieldInfo</param>
        /// <returns>GetterDelegate for property or field, null otherwise</returns>
        /// <remarks>
        /// Note: use with caution this method will expose private and protected constructors without safety checks.
        /// </remarks>
        public static GetterDelegate GetGetter(MemberInfo memberInfo)
        {
            if (memberInfo is PropertyInfo)
            {
                return(DynamicMethodGenerator.GetPropertyGetter((PropertyInfo)memberInfo));
            }

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

            return(null);
        }
        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 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));
        }