private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value) { if (value == null) { return; } Type pType = handle.MemberType; Type vType = GetUnderlyingType(value.GetType()); object v = CoerceValue(pType, vType, value); if (v != null) { handle.DynamicMemberSet(target, v); } }
/// <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)); }
internal static DynamicMemberHandle GetCachedField(Type objectType, string fieldName) { var key = new MethodCacheKey(objectType.FullName, fieldName, GetParameterTypes(null)); DynamicMemberHandle mh = null; if (_memberCache.TryGetValue(key, out mh)) { return(mh); } lock (_memberCache) { if (!_memberCache.TryGetValue(key, out mh)) { FieldInfo info = objectType.GetField(fieldName, fieldFlags); if (info != null) { mh = new DynamicMemberHandle(info); } _memberCache.Add(key, mh); } } return(mh); }
internal static DynamicMemberHandle GetCachedProperty(Type objectType, string propertyName) { var key = new MethodCacheKey(objectType.FullName, propertyName, GetParameterTypes(null)); DynamicMemberHandle mh = null; if (_memberCache.TryGetValue(key, out mh)) { return(mh); } lock (_memberCache) { if (!_memberCache.TryGetValue(key, out mh)) { PropertyInfo info = FindProperty(objectType, propertyName); if (info != null) { mh = new DynamicMemberHandle(info); } _memberCache.Add(key, mh); } } return(mh); }
/// <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); } } }
internal static DynamicMemberHandle GetCachedProperty(Type objectType, string propertyName) { var key = new MethodCacheKey(objectType.FullName, propertyName, GetParameterTypes(null)); DynamicMemberHandle mh = null; if (_memberCache.TryGetValue(key, out mh)) return mh; lock (_memberCache) { if (!_memberCache.TryGetValue(key, out mh)) { PropertyInfo info = FindProperty(objectType, propertyName); if (info != null) mh = new DynamicMemberHandle(info); _memberCache.Add(key, mh); } } return mh; }
internal static DynamicMemberHandle GetCachedField(Type objectType, string fieldName) { var key = new MethodCacheKey(objectType.FullName, fieldName, GetParameterTypes(null)); DynamicMemberHandle mh = null; if (_memberCache.TryGetValue(key, out mh)) return mh; lock (_memberCache) { if (!_memberCache.TryGetValue(key, out mh)) { FieldInfo info = objectType.GetField(fieldName, fieldFlags); if (info != null) mh = new DynamicMemberHandle(info); _memberCache.Add(key, mh); } } return mh; }
private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value) { if (value == null) return; Type pType = handle.MemberType; Type vType = GetUnderlyingType(value.GetType()); object v = CoerceValue(pType, vType, value); if (v != null) handle.DynamicMemberSet(target, v); }
/// <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 mh = GetCachedProperty(obj.GetType(), property); mh.DynamicMemberSet(obj, value); }
/// <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 mh = GetCachedProperty(obj.GetType(), property); return(mh.DynamicMemberGet(obj)); }