Beispiel #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)
     {
         return(MethodCaller.GetCachedProperty(target.GetType(), propertyName).DynamicMemberGet(target));
     }
     return(MethodCaller.FindProperty(target.GetType(), propertyName).GetValue(target, null));
 }
Beispiel #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 cachedProperty = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
         if (cachedProperty != null)
         {
             SetValueWithCoercion(target, cachedProperty, value);
         }
     }
     else
     {
         PropertyInfo handle = MethodCaller.FindProperty(target.GetType(), propertyName);
         if (handle != null)
         {
             SetValueWithCoercion(target, handle, value);
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// Invokes a property setter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <param name="value">New value for property.</param>
        public static void CallPropertySetter(object obj, string property, object value)
        {
            DynamicMemberHandle cachedProperty = MethodCaller.GetCachedProperty(obj.GetType(), property);

            cachedProperty.DynamicMemberSet(obj, value);
        }
Beispiel #4
0
        /// <summary>
        /// Invokes a property getter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <returns></returns>
        public static object CallPropertyGetter(object obj, string property)
        {
            DynamicMemberHandle cachedProperty = MethodCaller.GetCachedProperty(obj.GetType(), property);

            return(cachedProperty.DynamicMemberGet(obj));
        }