Пример #1
0
 private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value)
 {
     if (value != null)
     {
         Type   memberType     = handle.MemberType;
         Type   underlyingType = GetUnderlyingType(value.GetType());
         object arg            = CoerceValue(memberType, underlyingType, value);
         if (arg != null)
         {
             handle.DynamicMemberSet(target, arg);
         }
     }
 }
Пример #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);
         }
     }
 }
Пример #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);
        }
Пример #4
0
        internal static DynamicMemberHandle GetCachedField(Type objectType, string fieldName)
        {
            MethodCacheKey      key = new MethodCacheKey(objectType.FullName, fieldName, 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))
                {
                    FieldInfo field = objectType.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (field != null)
                    {
                        dynamicMemberHandle = new DynamicMemberHandle(field);
                    }
                    MethodCaller._memberCache.Add(key, dynamicMemberHandle);
                }
            }
            return(dynamicMemberHandle);
        }
Пример #5
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);
        }
Пример #6
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));
        }