示例#1
0
        /// <summary>
        /// Creates the property access item.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="propertyInfo">The property information.</param>
        /// <returns>The property access item.</returns>
        internal static PropertyAccessItem CreatePropertyAccessItem(Type objectType, PropertyInfo propertyInfo)
        {
            if (objectType == null)
            {
                throw new ArgumentNullException("objectType");
            }

            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }

            bool canRead  = propertyInfo.CanRead;
            bool canWrite = propertyInfo.CanWrite;

            PropertyAccessItem propertyAccessItem = new PropertyAccessItem
            {
                CanRead  = canRead,
                CanWrite = canWrite
            };

            if (canRead)
            {
                propertyAccessItem.Getter = DynamicMethodHelper.EmitPropertyGetter(objectType, propertyInfo);
            }

            if (canWrite)
            {
                propertyAccessItem.Setter = DynamicMethodHelper.EmitPropertySetter(objectType, propertyInfo);
            }

            return(propertyAccessItem);
        }
示例#2
0
        /// <summary>
        /// Sets the property value.
        /// </summary>
        /// <param name="object">The object.</param>
        /// <param name="propertyInfo">The property information.</param>
        /// <param name="value">The value.</param>
        public static void SetPropertyValue(object @object, PropertyInfo propertyInfo, object value)
        {
            if (@object == null)
            {
                throw new ArgumentNullException("object");
            }

            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }

            Type type = @object.GetType();

            if (!propertyInfo.CanWrite)
            {
                throw new ReflectionHelperException(string.Format(CultureInfo.CurrentCulture, Strings.ReflectionHelper_SetPropertyValue_the_property_has_no_set_method, propertyInfo.Name, type.AssemblyQualifiedName));
            }

            PropertyAccessItem propertyAccessItem = GetPropertyAccessItem(type, propertyInfo);

            //if (!TypeUtils.IsImplicitlyConvertible(valueType, propertyInfo.PropertyType))
            //{
            //    throw new ReflectionHelperException(string.Format(CultureInfo.CurrentCulture, Strings.ReflectionHelper_SetPropertyValue_type_is_not_implicitly_convertable, valueType, propertyInfo.PropertyType));
            //}

            CheckAreAssignable(propertyInfo, value, propertyInfo.PropertyType);

            propertyAccessItem.Setter(@object, value);
        }
示例#3
0
        /// <summary>
        /// Gets the property value.
        /// </summary>
        /// <param name="object">The object.</param>
        /// <param name="propertyInfo">The property information.</param>
        /// <returns>The property value</returns>
        /// <exception cref="System.ArgumentNullException">
        /// object
        /// or
        /// propertyInfo
        /// </exception>
        public static object GetPropertyValue(object @object, PropertyInfo propertyInfo)
        {
            if (@object == null)
            {
                throw new ArgumentNullException("object");
            }

            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }

            Type type = @object.GetType();

            if (!propertyInfo.CanRead)
            {
                throw new ReflectionHelperException(string.Format(CultureInfo.CurrentCulture, Strings.ReflectionHelper_GetPropertyValue_property_has_no_get_method, propertyInfo.Name, type.AssemblyQualifiedName));
            }

            PropertyAccessItem propertyAccessItem = GetPropertyAccessItem(type, propertyInfo);

            return(propertyAccessItem.Getter(@object));
        }