internal static GetHandler GetShadowGetter(MethodInfo method) { if (AccessorsCacheCheck.PropertiesDisabled) { return(inst => method.Invoke(inst, null)); } GetHandler getter; lock (ShadowGetters) { if (ShadowGetters.TryGetValue(method, out getter)) { return(getter); } } getter = DynamicMethodCompiler.CreateGetHandler(method.ReflectedType, method) ?? (inst => method.Invoke(inst, null)); lock (ShadowGetters) ShadowGetters[method] = getter; return(getter); }
private void ValidatePropertyGetter(string propertyName) { if (!_propertyGetters.ContainsKey(propertyName)) { _propertyGetters.Add(propertyName, DynamicMethodCompiler.CreateGetHandler(_type, _type.GetProperty(propertyName))); } }
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); }
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); }
private static void CheckAttributesNotNull(Type type, CommandBase command, IDictionary <Type, Dictionary <string, GetHandler> > getterDictionary, Type attributeType, string errorMsg, bool holderValues) { if (!getterDictionary.ContainsKey(type)) { getterDictionary[type] = new Dictionary <String, GetHandler>(); foreach (PropertyInfo property in type.GetProperties()) { if (property.GetCustomAttributes(attributeType, true).Length > 0) { getterDictionary[type][property.Name] = DynamicMethodCompiler.CreateGetHandler(type, property); } } } foreach (KeyValuePair <string, GetHandler> getHandler in getterDictionary[type]) { object value = getHandler.Value(command); if (value == null) { Debug.Fail(String.Format(errorMsg, command, getHandler.Key)); } if (holderValues) { IHolder holder = value as IHolder; if (holder != null) { if (holder.WrappedValue == null) { Debug.Fail(String.Format(errorMsg, command, getHandler.Key)); } } } } }
/// <summary> /// Examines the type of the bound object and initializes all neccessary structures /// that are needed before binding can start. This methods to be called once for /// each type passed as <paramref name="target"/> (not for each object of that type). /// </summary> /// <param name="target">bound object</param> /// <param name="sourceType">Type of binding (model/view helper).</param> private void PrepareBindingTo(IBindable target, EBindingSourceType sourceType) { if (sourceType == EBindingSourceType.Model) { PropertyInfo modelProperty = FindFieldByAttribute(Type, typeof(ModelElementAttribute)); ModelElementType = modelProperty.GetValue(target, null).GetType(); if (ModelElementType == null) { throw new InvalidOperationException(string.Format("Model property for type {0} is not assigned or the property is not marked with ModelElementAttribute", Type)); } if (!initializationInProgress) { Debug.WriteLine("Model binding for type " + target.GetType().Name + " with model type " + ModelElementType.Name); } ModelSourceGetter = DynamicMethodCompiler.CreateGetHandler(ModelElementType, modelProperty); CreateBindingFunctions(sourceType, ModelElementType); } if (sourceType == EBindingSourceType.View) { PropertyInfo viewHelperProperty = FindFieldByAttribute(Type, typeof(ViewHelperElementAttribute)); ViewHelperType = viewHelperProperty.GetValue(target, null).GetType(); if (ViewHelperType == null) { throw new InvalidOperationException(string.Format("ViewHelper for type {0} is not assigned or the property is not marked with ViewHelperElementAttribute", Type)); } if (!initializationInProgress) { Debug.WriteLine("View binding for type " + target.GetType().Name + " with view type " + ViewHelperType.Name); } ViewSourceGetter = DynamicMethodCompiler.CreateGetHandler(ViewHelperType, viewHelperProperty); CreateBindingFunctions(sourceType, ViewHelperType); } }
/// <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); } } }
public static Object GetFieldValue(Object obj, FieldInfo field) { //创建Set委托 GetHandler getter = DynamicMethodCompiler.CreateGetHandler(obj.GetType(), field); //获取字段值 return(getter(obj)); }
public static Object GetPropertyValue(Object obj, PropertyInfo property) { //创建Set委托 GetHandler getter = DynamicMethodCompiler.CreateGetHandler(obj.GetType(), property); //获取属性值 return(getter(obj)); }
private Dictionary <string, object> CalculteValues <T>(T parameter, IEnumerable <PropertyInfo> props) { Dictionary <string, object> values = new Dictionary <string, object>(); var type = typeof(T); foreach (var prop in props) { values.Add(prop.Name, DynamicMethodCompiler.CreateGetHandler(type, prop)(parameter)); } return(values); }
private void ValidateFieldGetter(string fieldName) { if (!_fieldGetters.ContainsKey(fieldName)) { FieldInfo fieldInfo = _type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (fieldInfo == null) { throw new ArgumentOutOfRangeException(fieldName, "Unable to find fieldname"); } _fieldGetters.Add(fieldName, DynamicMethodCompiler.CreateGetHandler(_type, fieldInfo)); } }
private IDbCommand CreateCommandByObject <T>(string commandText, T parameter) { var commd = Connection.CreateCommand(); commd.Transaction = Transaction; commd.CommandText = ReplaceProfixTag(commandText); commd.CommandTimeout = DefaultCommandTimeoutBySeconds; var commandType = commd.GetType(); var type = typeof(T); PropertyInfo[] props = type.GetProperties(); foreach (var prop in props) { var handler = DynamicMethodCompiler.CreateGetHandler(type, prop); commd.Parameters.Add(CreateIDataParameter(TagName + GetColumnName(prop), handler(parameter), ParameterDirection.Input)); } return(commd); }
public xCol(GridColumn gcol, RepositoryItemLookUpEditBase rle, TableDef td, Type ParentType) { this.rle = rle; if (td != null) { fldDisplay = td[rle.DisplayMember] ?? td[gcol.FieldName]; } if (fldDisplay == null) { DisplayMember = rle.DisplayMember; } if (ParentType != null) { GetSrcHandler = DynamicMethodCompiler.CreateGetHandler( ParentType.GetMember( ((BindingSource)rle.DataSource).DataMember)[0]); } }
private static void CheckAttributesNotNull(Type type, CommandBase command, IDictionary <Type, Dictionary <string, GetHandler> > getterDictionary, Type attributeType, string errorMsg) { #if SILVERLIGHT foreach (PropertyInfo property in type.GetProperties()) { if (property.GetCustomAttributes(attributeType, true).Length > 0) { object value = property.GetValue(command, null); if (value == null) { Debug.Assert(false, String.Format(errorMsg, command, property.Name)); } } } #else if (!getterDictionary.ContainsKey(type)) { getterDictionary[type] = new Dictionary <String, GetHandler>(); foreach (PropertyInfo property in type.GetProperties()) { object[] customAttributes = property.GetCustomAttributes(attributeType, true); if (customAttributes.Length > 0 && !((PublicArgumentAttribute)customAttributes[0]).AllowNullInput) { getterDictionary[type][property.Name] = DynamicMethodCompiler.CreateGetHandler(type, property); } } } foreach (KeyValuePair <string, GetHandler> getHandler in getterDictionary[type]) { object value = getHandler.Value(command); if (value == null) { Debug.Assert(false, String.Format(errorMsg, command, getHandler.Key)); } } #endif }
/// <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)); }