private void ValidatePropertySetter(string propertyName) { if (!_propertySetters.ContainsKey(propertyName)) { _propertySetters.Add(propertyName, DynamicMethodCompiler.CreateSetHandler(_type, _type.GetProperty(propertyName))); } }
internal static Accessors GetAccessors(PropertyInfo member) { if (AccessorsCacheCheck.PropertiesDisabled) { return(new Accessors(inst => Helpers.GetPropertyValue(member, inst), (inst, v) => member.SetValue(inst, v, null))); } Accessors accessors; lock (Properties) { if (Properties.TryGetValue(member, out accessors)) { return(accessors); } } GetHandler getter = DynamicMethodCompiler.CreateGetHandler(member.ReflectedType, member) ?? (inst => Helpers.GetPropertyValue(member, inst)); SetHandler setter = DynamicMethodCompiler.CreateSetHandler(member.ReflectedType, member) ?? ((inst, v) => member.SetValue(inst, v, null)); accessors = new Accessors(getter, setter); lock (Properties) Properties[member] = accessors; return(accessors); }
internal static SetHandler GetShadowSetter(MethodInfo method) { if (AccessorsCacheCheck.PropertiesDisabled) { return((inst, v) => method.Invoke(inst, new[] { v })); } SetHandler setter; lock (ShadowSetters) { if (ShadowSetters.TryGetValue(method, out setter)) { return(setter); } } setter = DynamicMethodCompiler.CreateSetHandler(method.ReflectedType, method) ?? ((inst, v) => method.Invoke(inst, new[] { v })); lock (ShadowSetters) ShadowSetters[method] = setter; return(setter); }
static AccessorsCacheCheck() { var test = new TestClass(); try { DynamicMethodCompiler.CreateSetHandler( typeof(AccessorsCache), typeof(TestClass).GetField("x", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))(test, 55); } catch { } if (test.GetX() != 55) { FieldsDisabled = true; } try { DynamicMethodCompiler.CreateSetHandler( typeof(AccessorsCache), typeof(TestClass).GetProperty("Y", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))(test, 55); } catch { } if (test.GetY() != 55) { PropertiesDisabled = true; } }
internal static Accessors GetAccessors(FieldInfo member) { if (AccessorsCacheCheck.PropertiesDisabled) { return(new Accessors(member.GetValue, member.SetValue)); } Accessors accessors; lock (Fields) { if (Fields.TryGetValue(member, out accessors)) { return(accessors); } } GetHandler getter = DynamicMethodCompiler.CreateGetHandler(member.ReflectedType, member) ?? member.GetValue; SetHandler setter = DynamicMethodCompiler.CreateSetHandler(member.ReflectedType, member) ?? member.SetValue; accessors = new Accessors(getter, setter); lock (Fields) Fields[member] = accessors; return(accessors); }
/// <summary> /// Creates the binding functions (fills <see cref="ViewSetterCollection"/> and /// <see cref="ViewGetterCollection"/> or <see cref="ModelSetterCollection"/> and /// <see cref="ModelGetterCollection"/> with correct handlers). The handlers are /// dynamically compiled. /// </summary> /// <param name="sourceType">Type of the source to bound to</param> /// <param name="boundType">Type of the bound object</param> private void CreateBindingFunctions(EBindingSourceType sourceType, Type boundType) { if (sourceType == EBindingSourceType.View) { foreach (ViewHelperPropertyMappingAttribute attribute in ViewMappingDeclarations) { if (!initializationInProgress) { Debug.WriteLine("View binding: " + attribute.SourcePropertyName + " -> " + attribute.declaringProperty.Name); } PropertyInfo sourceProp = boundType.GetProperty(attribute.SourcePropertyName); ViewGetterCollection[sourceProp.Name] = DynamicMethodCompiler.CreateGetHandler(boundType, sourceProp); ViewSetterCollection[sourceProp.Name] = DynamicMethodCompiler.CreateSetHandler(Type, attribute.declaringProperty); } } if (sourceType == EBindingSourceType.Model) { foreach (ModelPropertyMappingAttribute attribute in ModelMappingDeclarations) { if (!initializationInProgress) { Debug.WriteLine("Model binding: " + attribute.SourcePropertyName + " -> " + attribute.declaringProperty.Name); } PropertyInfo sourceProp = boundType.GetProperty(attribute.SourcePropertyName); ModelGetterCollection[sourceProp.Name] = DynamicMethodCompiler.CreateGetHandler(boundType, sourceProp); ModelSetterCollection[sourceProp.Name] = DynamicMethodCompiler.CreateSetHandler(Type, attribute.declaringProperty); } } }
private void BuildSetter() { Setter = new SetHandler[rdr.FieldCount]; EnumType = new Type[rdr.FieldCount]; Type tp = typeof(TEntity); TableDef td = MetaData.GetTableDef(tp); for (int i = 0; i < rdr.FieldCount; i++) { string Name = rdr.GetName(i); FieldDef fld = td.GetFieldDef(Name); if (fld != null) { Setter[i] = fld.GetLoadSetHandler(); if (fld.IsEnum) { EnumType[i] = fld.FieldType; } } else { MemberInfo[] mis = tp.GetMember("_" + Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (mis.Length == 0) { mis = tp.GetMember(Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } if (mis.Length == 0) { Setter[i] = null; } else { if (mis[0].MemberType == MemberTypes.Field) { Type ft = ((FieldInfo)mis[0]).FieldType; if (ft.IsEnum) { EnumType[i] = ft; } } else { Type ft = ((PropertyInfo)mis[0]).PropertyType; if (ft.IsEnum) { EnumType[i] = ft; } } Setter[i] = DynamicMethodCompiler.CreateSetHandler(mis[0]); } } } }
private void ValidateFieldSetter(string fieldName) { if (!_fieldSetters.ContainsKey(fieldName)) { FieldInfo fieldInfo = _type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (fieldInfo == null) { throw new ArgumentOutOfRangeException(fieldName, "Unable to find fieldname"); } _fieldSetters.Add(fieldName, DynamicMethodCompiler.CreateSetHandler(_type, fieldInfo)); } }
public static void SetFieldValue(Object obj, FieldInfo field, Object value) { //创建Set委托 SetHandler setter = DynamicMethodCompiler.CreateSetHandler(obj.GetType(), field); //先获取该私有成员的数据类型 Type type = field.FieldType; //通过数据类型转换 value = TypeUtils.ConvertForType(value, type); //将值设置到对象中 setter(obj, value); }
public static void SetPropertyValue(Object obj, PropertyInfo property, Object value) { //创建Set委托 SetHandler setter = DynamicMethodCompiler.CreateSetHandler(obj.GetType(), property); //先获取该私有成员的数据类型 Type type = property.PropertyType; //通过数据类型转换 value = TypeUtils.ConvertForType(value, type); //将值设置到对象中 setter(obj, value); }
private void AssignValue <T>(IDataReader reader, IEnumerable <string> allFieldNames, PropertyInfo propertyInfo, T target) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (propertyInfo == null) { throw new ArgumentNullException(nameof(propertyInfo)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } string col = GetColumnName(propertyInfo); string typeName = typeof(T).FullName; if (allFieldNames.Where(a => a.Equals(col, StringComparison.InvariantCultureIgnoreCase)).Any()) { var index = reader.GetOrdinal(col); var value = reader.GetValue(index); if (value == DBNull.Value) { value = null; } if (index > -1) { var type = typeof(T); if (propertyInfo.PropertyType.IsEnum) { DynamicMethodCompiler.CreateSetHandler(type, propertyInfo)(target, Enum.ToObject(propertyInfo.PropertyType, value)); } else { var handler = DynamicMethodCompiler.CreateSetHandler(type, propertyInfo); handler(target, ConvertTo(propertyInfo.PropertyType, value)); //try //{ // handler(target, value); //} //catch //{ // handler(target, ConvertTo(propertyInfo.PropertyType, value)); //} } } } }
/// <summary> /// Creates new <see cref="PropertyBinder"/>, bject that copies value of a /// property from one object to another object each time the value /// changes. /// </summary> /// <param name="source">Source object</param> /// <param name="target">Target object</param> /// <param name="sourceField">Field of the <paramref name="source"/> that is bound to <paramref name="targetField"/></param> /// <param name="targetField">Field of the <paramref name="target"/> that recieves updated values of from <paramref name="sourceField"/></param> public PropertyBinder(INotifyPropertyChanged source, object target, string sourceField, string targetField, Dictionary <KeyValuePair <Type, string>, GetHandler> getCache, Dictionary <KeyValuePair <Type, string>, SetHandler> setCache) { Source = source; Target = target; SourceField = sourceField; TargetField = targetField; Source.PropertyChanged += Source_PropertyChanged; KeyValuePair <Type, string> keyGet = new KeyValuePair <Type, string>(source.GetType(), sourceField); KeyValuePair <Type, string> keySet = new KeyValuePair <Type, string>(target.GetType(), targetField); if (getCache.ContainsKey(keyGet)) { sourceGetHandler = getCache[keyGet]; } else { PropertyInfo sourceProp = Source.GetType().GetProperty(SourceField); sourceGetHandler = DynamicMethodCompiler.CreateGetHandler(Source.GetType(), sourceProp); /**/ getCache[keyGet] = sourceGetHandler; } if (getCache.ContainsKey(keySet)) { targetSetHandler = setCache[keySet]; } if (targetSetHandler == null) { PropertyInfo targetProp = Target.GetType().GetProperty(TargetField); /**/ targetSetHandler = DynamicMethodCompiler.CreateSetHandler(Target.GetType(), targetProp); /**/ setCache[keySet] = targetSetHandler; } //Debug.Assert(sourceProp != null, "Source property not found"); //Debug.Assert(targetProp != null, "Target property not found"); Source_PropertyChanged(null, new PropertyChangedEventArgs(SourceField)); }