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 GetPropertyGetter_NullInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(null);
            });

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

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

            Assert.NotNull(propertyInfo);

            GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);

            Assert.Null(getter);
        }
        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));
        }
Exemple #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 GetPropertyGetter_1MillionPropertyGets_PerformsInAround10ms()
        {
            Example      instance     = new Example();
            PropertyInfo propertyInfo = typeof(Example).GetProperty("A", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            GetterDelegate getter = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);

            string value = null;

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