Ejemplo n.º 1
0
        /// <summary>
        /// Gets an object's property value by name.
        /// </summary>
        /// <param name="target">Object containing the property to get.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="useCache">if set to <c>true</c> use dynamic cache.</param>
        /// <returns>The value of the property.</returns>
        public static object GetPropertyValue(object target, string propertyName, bool useCache)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (String.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            if (useCache)
            {
                DynamicMemberHandle handle = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
                return(handle.DynamicMemberGet(target));
            }

            PropertyInfo propertyInfo = MethodCaller.FindProperty(target.GetType(), propertyName);

            return(propertyInfo.GetValue(target, null));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets an object's property with the specified value,
        /// converting that value to the appropriate type if possible.
        /// </summary>
        /// <param name="target">Object containing the property to set.</param>
        /// <param name="propertyName">Name of the property to set.</param>
        /// <param name="value">Value to set into the property.</param>
        /// <param name="useCache">if set to <c>true</c> use dynamic cache.</param>
        public static void SetPropertyValue(object target, string propertyName, object value, bool useCache)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target", "Target object can not be Null.");
            }

            if (useCache)
            {
                DynamicMemberHandle handle = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
                if (handle != null)
                {
                    SetValueWithCoercion(target, handle, value);
                }
            }
            else
            {
                PropertyInfo propertyInfo = MethodCaller.FindProperty(target.GetType(), propertyName);
                if (propertyInfo != null)
                {
                    SetValueWithCoercion(target, propertyInfo, value);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Finds a <see cref="PropertyInfo"/> by name ignoring case.
 /// </summary>
 /// <param name="type">The type to search.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <returns>A <see cref="PropertyInfo"/> matching the property name.</returns>
 /// <remarks>
 /// FindProperty will first try to get a property matching the name and case of the
 /// property name specified.  If a property cannot be found, all the properties will
 /// be searched ignoring the case of the name.
 /// </remarks>
 public static PropertyInfo FindProperty(Type type, string propertyName)
 {
     return(MethodCaller.FindProperty(type, propertyName));
 }