Exemplo 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)
     {
         return(MethodCaller.GetCachedProperty(target.GetType(), propertyName).DynamicMemberGet(target));
     }
     return(MethodCaller.FindProperty(target.GetType(), propertyName).GetValue(target, null));
 }
Exemplo 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 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);
         }
     }
 }
Exemplo n.º 3
0
        internal static DynamicMemberHandle GetCachedProperty(Type objectType, string propertyName)
        {
            MethodCacheKey      key = new MethodCacheKey(objectType.FullName, propertyName, MethodCaller.GetParameterTypes(null));
            DynamicMemberHandle dynamicMemberHandle = null;

            if (MethodCaller._memberCache.TryGetValue(key, out dynamicMemberHandle))
            {
                return(dynamicMemberHandle);
            }
            lock (MethodCaller._memberCache)
            {
                if (!MethodCaller._memberCache.TryGetValue(key, out dynamicMemberHandle))
                {
                    PropertyInfo propertyInfo = MethodCaller.FindProperty(objectType, propertyName);
                    if (propertyInfo != null)
                    {
                        dynamicMemberHandle = new DynamicMemberHandle(propertyInfo);
                    }
                    MethodCaller._memberCache.Add(key, dynamicMemberHandle);
                }
            }
            return(dynamicMemberHandle);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Finds a <see cref="T:System.Reflection.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="T:System.Reflection.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));
 }