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 Map(ResolutionContext context) { var result = new ResolutionResult(context); foreach (var resolver in AllResolvers()) { result.MemberBindings.AddRange(resolver.Resolve(context).MemberBindings); } return result; }
public ResolutionResult Map(ResolutionContext context) { var result = new ResolutionResult(context); foreach (var resolver in AllResolvers()) { result.MemberBindings.AddRange(resolver.Resolve(context).MemberBindings); } return(result); }
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 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; }
private static Func <TSource, TTarget, TTarget> B2() { Type sourceType = typeof(TSource); Type targetType = typeof(TTarget); ParameterExpression sourceParameter = Expression.Parameter(sourceType, "source"); IConfigurationProvider provider = DefaultConfigurationProvider.Current; IMappingRunner runner = provider.FindRunner(sourceType); ResolutionContext context = new ResolutionContext(sourceType, targetType, sourceParameter); ResolutionResult result = runner.Map(context); Expression initializer = Expression.MemberInit(Expression.New(targetType), result.MemberBindings); Func <TSource, TTarget, TTarget> func = Expression.Lambda <Func <TSource, TTarget, TTarget> >(initializer, sourceParameter).Compile(); return(func); }