public ResolutionResult Resolve(ResolutionContext context) { var propertyResult = new ResolutionResult(context); var bindings = new List <MemberBinding>(); var targetProperties = from property in context.TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance) where property.CanWrite select property; foreach (PropertyInfo targetProperty in targetProperties) { MappingAttribute attribute = targetProperty.GetAttribute(); if (attribute != null && attribute.Ignored) { continue; } PropertyInfo sourceProperty = context.SourceType.GetProperty(targetProperty.Name); if (sourceProperty == null) { if (attribute != null) { sourceProperty = context.SourceType.GetProperty(attribute.Name); } if (sourceProperty == null) { continue; } } if (!sourceProperty.CanRead) { continue; } MemberBinding memberBinding; if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType)) { memberBinding = Expression.Bind( targetProperty, Expression.Convert( Expression.Property(context.Parameter, sourceProperty), targetProperty.PropertyType) ); } else { memberBinding = Expression.Bind(targetProperty, Expression.Property(context.Parameter, sourceProperty)); } bindings.Add(memberBinding); } propertyResult.MemberBindings = bindings; return(propertyResult); }
public ResolutionResult Resolve(ResolutionContext context) { ResolutionResult fieldResult = new ResolutionResult(context); List <MemberBinding> bindings = new List <MemberBinding>(); var targetFields = from field in context.TargetType.GetFields(BindingFlags.Public | BindingFlags.Instance) select field; foreach (FieldInfo targetField in targetFields) { MappingAttribute attribute = targetField.GetAttribute(); if (attribute != null && attribute.Ignored) { continue; } FieldInfo sourceField = context.SourceType.GetField(targetField.Name); if (sourceField == null) { if (attribute != null) { sourceField = context.SourceType.GetField(attribute.Name); } if (sourceField == null) { continue; } } MemberBinding memberBinding; if (!targetField.FieldType.IsAssignableFrom(sourceField.FieldType)) { memberBinding = Expression.Bind( targetField, Expression.Convert( Expression.Field(context.Parameter, sourceField), targetField.FieldType) ); } else { memberBinding = Expression.Bind(targetField, Expression.Field(context.Parameter, sourceField)); } bindings.Add(memberBinding); } fieldResult.MemberBindings = bindings; return(fieldResult); }
public static MappingAttribute GetAttribute(this MemberInfo member) { MappingAttribute[] attributes = member.GetCustomAttributes <MappingAttribute>(); MappingAttribute attribute = null; if (attributes.Length != 0) { attribute = attributes[0]; } return(attribute); }