private string GetTargetName( Option<BindingConfig> bindingConfig, TypePair typePair, MemberInfo sourceMember, Dictionary<string, string> targetBindings) { Option<string> targetName; List<BindAttribute> binds = sourceMember.GetAttributes<BindAttribute>(); BindAttribute bind = binds.FirstOrDefault(x => x.TargetType.IsNull()); if (bind.IsNull()) { bind = binds.FirstOrDefault(x => typePair.Target.IsAssignableFrom(x.TargetType)); } if (bind.IsNotNull()) { targetName = new Option<string>(bind.MemberName); } else { targetName = bindingConfig.Map(x => x.GetBindField(sourceMember.Name)); if (targetName.HasNoValue) { string targetMemberName; if (targetBindings.TryGetValue(sourceMember.Name, out targetMemberName)) { targetName = new Option<string>(targetMemberName); } else { targetName = new Option<string>(sourceMember.Name); } } } return targetName.Value; }
public bool IsMatch(TypePair context) { // destination type must be IEnumerable interface or a class implementing at least IList return ((context.DestinationType.IsInterface() && context.DestinationType.IsEnumerableType()) || context.DestinationType.IsListType()) && context.SourceType.IsEnumerableType(); }
private void DryRunTypeMap(ICollection<TypeMap> typeMapsChecked, TypePair types, TypeMap typeMap, ResolutionContext context) { if (typeMap != null) { typeMapsChecked.Add(typeMap); if(typeMap.CustomMapper != null || typeMap.TypeConverterType != null) { return; } CheckPropertyMaps(typeMapsChecked, typeMap, context); } else { var mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(types)); if (mapperToUse == null && types.SourceType.IsNullableType()) { var nullableTypes = new TypePair(Nullable.GetUnderlyingType(types.SourceType), types.DestinationType); mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableTypes)); } if (mapperToUse == null) { throw new AutoMapperConfigurationException(types); } if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper) { CheckElementMaps(typeMapsChecked, types, context); } } }
private static Option<Func<object, object>> ConvertEnum(TypePair pair) { Func<object, object> result; if (pair.IsEnumTypes) { result = x => Convert.ChangeType(x, pair.Source); return result.ToOption(); } if (pair.Target.IsEnum) { if (pair.Source.IsEnum == false) { if (pair.Source == typeof(string)) { result = x => Enum.Parse(pair.Target, x.ToString()); } } result = x => Enum.ToObject(pair.Target, Convert.ChangeType(x, Enum.GetUnderlyingType(pair.Target))); return result.ToOption(); } if (pair.Source.IsEnum) { result = x => Convert.ChangeType(x, pair.Target); return result.ToOption(); } return Option<Func<object, object>>.Empty; }
private MapperCacheItem CreateMapperCacheItem(TypePair typePair) { MapperBuilder mapperBuilder = GetMapperBuilder(typePair); Mapper mapper = mapperBuilder.Build(typePair); MapperCacheItem mapperCacheItem = _mapperCache.Add(typePair, mapper); return mapperCacheItem; }
private static bool EnumToEnumMapping(TypePair context) { // Enum to enum mapping var sourceEnumType = TypeHelper.GetEnumerationType(context.SourceType); var destEnumType = TypeHelper.GetEnumerationType(context.DestinationType); return sourceEnumType != null && destEnumType != null; }
public bool IsMatch(TypePair context) { return IsPrimitiveArrayType(context.DestinationType) && IsPrimitiveArrayType(context.SourceType) && (TypeHelper.GetElementType(context.DestinationType) .Equals(TypeHelper.GetElementType(context.SourceType))); }
private void DryRunTypeMap(ICollection<TypeMap> typeMapsChecked, ResolutionContext context) { var typeMap = context.TypeMap; if (typeMap != null) { typeMapsChecked.Add(typeMap); CheckPropertyMaps(typeMapsChecked, context); } else { var mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(context.Types)); if (mapperToUse == null && context.SourceType.IsNullableType()) { var nullableTypes = new TypePair(Nullable.GetUnderlyingType(context.SourceType), context.DestinationType); mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableTypes)); } if (mapperToUse == null) { throw new AutoMapperConfigurationException(context); } if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper) { CheckElementMaps(typeMapsChecked, context); } } }
public MapperBuilder GetMapperBuilder(TypePair parentTypePair, MappingMember mappingMember) { if (_customTypeMapperBuilder.IsSupported(parentTypePair, mappingMember)) { return _customTypeMapperBuilder; } return GetTypeMapperBuilder(mappingMember.TypePair); }
public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList, ProfileMap profile) { SourceTypeDetails = sourceType; DestinationTypeDetails = destinationType; Types = new TypePair(sourceType.Type, destinationType.Type); Profile = profile; ConfiguredMemberList = memberList; }
public MemberEmitterDescription Build(TypePair parentTypePair, List<MappingMember> members) { var emitter = new EmitComposite(); members.ForEach(x => emitter.Add(Build(parentTypePair, x))); var result = new MemberEmitterDescription(emitter, _mapperCache); result.AddMapper(_mapperCache); return result; }
private MapperCacheItem CreateMapperCacheItem(TypePair parentTypePair, MappingMember mappingMember) { MapperBuilder mapperBuilder = _config.GetMapperBuilder(parentTypePair, mappingMember); Mapper mapper = mapperBuilder.Build(parentTypePair, mappingMember); MapperCacheItem mapperCacheItem = _mapperCache.Add(mappingMember.TypePair, mapper); return mapperCacheItem; }
public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList) { _sourceType = sourceType; _destinationType = destinationType; Types = new TypePair(sourceType.Type, destinationType.Type); Profile = ConfigurationStore.DefaultProfileName; ConfiguredMemberList = memberList; }
public bool IsSupported(TypePair parentTypePair, MappingMember mappingMember) { Option<BindingConfig> bindingConfig = _config.GetBindingConfig(parentTypePair); if (bindingConfig.HasNoValue) { return false; } return bindingConfig.Value.HasCustomTypeConverter(mappingMember.Target.Name); }
public bool IsMatch(TypePair context) { if (!(context.SourceType.IsEnumerableType() && context.DestinationType.IsGenericType())) return false; var genericType = context.DestinationType.GetGenericTypeDefinition(); return genericType == typeof (ReadOnlyCollection<>); }
public void ReturnsIsFalseWhenSourceTypeIsNotNameValueCollection() { var tp = new TypePair(typeof(Object), typeof(NameValueCollection)); var nvcm = new NameValueCollectionMapper(); var result = nvcm.IsMatch(tp); result.ShouldBeFalse(); }
public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList, IProfileConfiguration profile) { SourceTypeDetails = sourceType; DestinationTypeDetails = destinationType; Types = new TypePair(sourceType.Type, destinationType.Type); Profile = profile; ConfiguredMemberList = memberList; IgnorePropertiesStartingWith = profile.GlobalIgnores; }
protected override bool IsSupportedCore(TypePair typePair) { return typePair.Source.IsPrimitive || typePair.Source == typeof(string) || typePair.Source == typeof(Guid) || typePair.Source.IsEnum || typePair.Source == typeof(decimal) || typePair.HasTypeConverter(); }
public void ReturnsTrueWhenBothSourceAndDestinationTypesAreNameValueCollection() { var tp = new TypePair(typeof(NameValueCollection), typeof(NameValueCollection)); var nvcm = new NameValueCollectionMapper(); var result = nvcm.IsMatch(tp); result.ShouldBeTrue(); }
public bool IsMatch(TypePair context) { var sourceTypeConverter = GetTypeConverter(context.SourceType); var destTypeConverter = GetTypeConverter(context.DestinationType); return sourceTypeConverter.CanConvertTo(context.DestinationType) || (context.DestinationType.IsNullableType() && sourceTypeConverter.CanConvertTo(Nullable.GetUnderlyingType(context.DestinationType)) || destTypeConverter.CanConvertFrom(context.SourceType)); }
public bool IsMatch(TypePair context) { var sourceEnumType = TypeHelper.GetEnumerationType(context.SourceType); var destEnumType = TypeHelper.GetEnumerationType(context.DestinationType); return sourceEnumType != null && destEnumType != null && sourceEnumType.GetCustomAttributes(typeof (FlagsAttribute), false).Any() && destEnumType.GetCustomAttributes(typeof (FlagsAttribute), false).Any(); }
/// <summary> /// /// </summary> /// <param name="typePair"></param> /// <returns></returns> private static MethodInfo GetImplicitConversionOperator(TypePair typePair) { var targetType = typePair.TargetType.IsNullableType() ? Nullable.GetUnderlyingType(typePair.TargetType) : typePair.TargetType; var sourceTypeMethod = typePair.SourceType .GetMethods(BindingFlags.Static) .FirstOrDefault(mi => mi.Name == "op_Implicit" && mi.ReturnType == targetType); return sourceTypeMethod ?? targetType .GetMethods(BindingFlags.Static) .FirstOrDefault(mi => mi.Name == "op_Implicit" && mi.GetParameters().Length == 1 && mi.GetParameters()[0].ParameterType == typePair.SourceType); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var contextTypePair = new TypePair(context.SourceType, context.DestinationType); Func<TypePair, IObjectMapper> missFunc = tp => context.Engine.ConfigurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(context)); var typeMap = mapper.ConfigurationProvider.CreateTypeMap(context.SourceType, context.DestinationType, _profileName); context = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType); var map = (context.Engine as MappingEngine)._objectMapperCache.GetOrAdd(contextTypePair, missFunc); return map.Map(context, mapper); }
protected override Mapper BuildCore(TypePair typePair) { Type parentType = typeof(ClassMapper<,>).MakeGenericType(typePair.Source, typePair.Target); TypeBuilder typeBuilder = _assembly.DefineType(GetMapperFullName(), parentType); EmitCreateTargetInstance(typePair.Target, typeBuilder); Option<MapperCache> mappers = EmitMapClass(typePair, typeBuilder); var result = (Mapper)Activator.CreateInstance(typeBuilder.CreateType()); mappers.Do(x => result.AddMappers(x.Mappers)); return result; }
private IEmitterType ConvertMember(TypePair parentTypePair, MappingMember member, IEmitterType sourceMemeber, IEmitterType targetMember) { if (member.TypePair.IsDeepCloneable && _config.GetBindingConfig(parentTypePair).HasNoValue) { return sourceMemeber; } MapperCacheItem mapperCacheItem = CreateMapperCacheItem(parentTypePair, member); IEmitterType result = mapperCacheItem.EmitMapMethod(sourceMemeber, targetMember); return result; }
private MapperBuilder GetTypeMapperBuilder(TypePair typePair) { if (_convertibleTypeMapperBuilder.IsSupported(typePair)) { return _convertibleTypeMapperBuilder; } else if (_collectionMapperBuilder.IsSupported(typePair)) { return _collectionMapperBuilder; } return _classMapperBuilder; }
private static MethodInfo GetExplicitConversionOperator(TypePair context) { var sourceTypeMethod = context.SourceType .GetDeclaredMethods() .Where(mi => mi.IsPublic && mi.IsStatic) .Where(mi => mi.Name == "op_Explicit") .FirstOrDefault(mi => mi.ReturnType == context.DestinationType); var destTypeMethod = context.DestinationType.GetDeclaredMethod("op_Explicit", new[] {context.SourceType}); return sourceTypeMethod ?? destTypeMethod; }
private IEmitter Build(TypePair parentTypePair, MappingMember member) { IEmitterType sourceObject = EmitArgument.Load(member.TypePair.Source, 1); IEmitterType targetObject = EmitArgument.Load(member.TypePair.Target, 2); IEmitterType sourceMember = LoadMember(member.Source, sourceObject); IEmitterType targetMember = LoadMember(member.Target, targetObject); IEmitterType convertedMember = ConvertMember(parentTypePair, member, sourceMember, targetMember); IEmitter result = StoreTargetObjectMember(member, targetObject, convertedMember); return result; }
private static MethodInfo GetImplicitConversionOperator(TypePair context) { var destinationType = context.DestinationType; if(destinationType.IsNullableType()) { destinationType = destinationType.GetTypeOfNullable(); } var sourceTypeMethod = context.SourceType .GetDeclaredMethods() .FirstOrDefault(mi => mi.IsPublic && mi.IsStatic && mi.Name == "op_Implicit" && mi.ReturnType == destinationType); return sourceTypeMethod ?? destinationType.GetMethod("op_Implicit", new[] { context.SourceType }); }
public static bool IsSubclassOrEqual(Type t1, Type t2) { if (t1 == null) throw new ArgumentNullException(); if (t2 == null) throw new ArgumentNullException(); if (t1.Equals(t2)) return true; bool result; TypePair typePair = new TypePair(t1, t2); if (!subTypeCached.TryGetValue(typePair, out result)) { result = subTypeCached[typePair] = t1.IsSubclassOf(t2); } return result; }
public Expression MapExpression(TypePair typePair, Expression sourceParameter, PropertyMap propertyMap = null, Expression destinationParameter = null) { return(MapExpression(_typeMapRegistry, _configurationProvider, typePair, sourceParameter, _context, propertyMap, destinationParameter)); }
public bool IsMatch(TypePair context) { var methodInfo = GetExplicitConversionOperator(context); return(methodInfo != null); }
public bool IsMatch(TypePair context) { return(context.SourceType.IsNullableType()); }
private TypeMap FindTypeMap(object source, object destination, Type sourceType, Type destinationType, string profileName) { TypeMap typeMap = FindExplicitlyDefinedTypeMap(sourceType, destinationType); if (typeMap == null && destinationType.IsNullableType()) { typeMap = FindExplicitlyDefinedTypeMap(sourceType, destinationType.GetTypeOfNullable()); } if (typeMap == null) { typeMap = _typeMaps.FirstOrDefault(x => x.SourceType == sourceType && x.GetDerivedTypeFor(sourceType) == destinationType); if (typeMap == null) { foreach (var sourceInterface in sourceType.GetInterfaces()) { typeMap = ((IConfigurationProvider)this).FindTypeMapFor(source, destination, sourceInterface, destinationType); if (typeMap == null) { continue; } var derivedTypeFor = typeMap.GetDerivedTypeFor(sourceType); if (derivedTypeFor != destinationType) { typeMap = CreateTypeMap(sourceType, derivedTypeFor, profileName, typeMap.ConfiguredMemberList); } break; } if ((sourceType.BaseType() != null) && (typeMap == null)) { typeMap = ((IConfigurationProvider)this).FindTypeMapFor(source, destination, sourceType.BaseType(), destinationType); } } } if (typeMap == null && sourceType.IsGenericType() && destinationType.IsGenericType()) { var sourceGenericDefinition = sourceType.GetGenericTypeDefinition(); var destGenericDefinition = destinationType.GetGenericTypeDefinition(); if (sourceGenericDefinition == null || destGenericDefinition == null) { return(null); } var genericTypePair = new TypePair(sourceGenericDefinition, destGenericDefinition); CreateTypeMapExpression genericTypeMapExpression; if (_typeMapExpressionCache.TryGetValue(genericTypePair, out genericTypeMapExpression)) { typeMap = CreateTypeMap(sourceType, destinationType, genericTypeMapExpression.ProfileName, genericTypeMapExpression.MemberList); var mappingExpression = CreateMappingExpression(typeMap, destinationType); genericTypeMapExpression.Accept(mappingExpression); } } return(typeMap); }
public bool IsMatch(TypePair context) => context.DestinationType.IsDictionaryType() && context.SourceType.IsEnumerableType() && !context.SourceType.IsDictionaryType();
/// <summary> /// When true, the mapping engine will use this mapper as the strategy /// </summary> /// <param name="context">Resolution context</param> /// <returns> /// Is match /// </returns> public override bool IsMatch(TypePair context) { return(typeof(Item).IsAssignableFrom(context.SourceType) && (context.DestinationType == typeof(string))); }
private void EmitDictionaryToDictionary(Type parentType, TypeBuilder typeBuilder, TypePair typePair) { EmitDictionaryToTarget(parentType, typeBuilder, typePair, DictionaryToDictionaryMethod, DictionaryToDictionaryTemplateMethod); }
public DefaultTypeMapConfig(TypePair types) { Types = types; }
private void EmitEnumerableToTarget(Type parentType, TypeBuilder typeBuilder, TypePair typePair, string methodName, string templateMethodName) { MethodBuilder methodBuilder = typeBuilder.DefineMethod(methodName, OverrideProtected, typePair.Target, new[] { typeof(IEnumerable) }); Type sourceItemType = typePair.Source.GetCollectionItemType(); Type targetItemType = typePair.Target.GetCollectionItemType(); EmitConvertItem(typeBuilder, new TypePair(sourceItemType, targetItemType)); MethodInfo methodTemplate = parentType.GetGenericMethod(templateMethodName, targetItemType); IEmitterType returnValue = EmitMethod.Call(methodTemplate, EmitThis.Load(parentType), EmitArgument.Load(typeof(IEnumerable), 1)); EmitReturn.Return(returnValue).Emit(new CodeGenerator(methodBuilder.GetILGenerator())); }
private static IEquivilentExpression GetEquivilentExpression(TypePair typePair) { return(EquivilentExpressions.GetEquivilentExpression(TypeHelper.GetElementType(typePair.SourceType), TypeHelper.GetElementType(typePair.DestinationType))); }
private void EmitEnumerableToArray(Type parentType, TypeBuilder typeBuilder, TypePair typePair) { EmitEnumerableToTarget(parentType, typeBuilder, typePair, EnumerableToArrayMethod, EnumerableToArrayTemplateMethod); }
private void EmitDictionaryToTarget(Type parentType, TypeBuilder typeBuilder, TypePair typePair, string methodName, string templateMethodName) { MethodBuilder methodBuilder = typeBuilder.DefineMethod(methodName, OverrideProtected, typePair.Target, new[] { typeof(IEnumerable) }); KeyValuePair <Type, Type> sourceTypes = typePair.Source.GetDictionaryItemTypes(); KeyValuePair <Type, Type> targetTypes = typePair.Target.GetDictionaryItemTypes(); EmitConvertItem(typeBuilder, new TypePair(sourceTypes.Key, targetTypes.Key), ConvertItemKeyMethod); EmitConvertItem(typeBuilder, new TypePair(sourceTypes.Value, targetTypes.Value)); var arguments = new[] { sourceTypes.Key, sourceTypes.Value, targetTypes.Key, targetTypes.Value }; MethodInfo methodTemplate = parentType.GetGenericMethod(templateMethodName, arguments); IEmitterType returnValue = EmitMethod.Call(methodTemplate, EmitThis.Load(parentType), EmitArgument.Load(typeof(IEnumerable), 1)); EmitReturn.Return(returnValue).Emit(new CodeGenerator(methodBuilder.GetILGenerator())); }
public bool IsMatch(TypePair context) => context.SourceType.IsDynamic() && !context.DestinationType.IsDynamic();
private Expression CreatePropertyMapFunc(PropertyMap propertyMap) { var destMember = MakeMemberAccess(_destination, propertyMap.DestinationProperty); Expression getter; var pi = propertyMap.DestinationProperty as PropertyInfo; if (pi != null && pi.GetGetMethod(true) == null) { getter = Default(propertyMap.DestinationPropertyType); } else { getter = destMember; } Expression destValueExpr; if (propertyMap.UseDestinationValue) { destValueExpr = getter; } else { if (_initialDestination.Type.IsValueType()) { destValueExpr = Default(propertyMap.DestinationPropertyType); } else { destValueExpr = Condition(Equal(_initialDestination, Constant(null)), Default(propertyMap.DestinationPropertyType), getter); } } var valueResolverExpr = BuildValueResolverFunc(propertyMap, getter); var resolvedValue = Variable(valueResolverExpr.Type, "resolvedValue"); var setResolvedValue = Assign(resolvedValue, valueResolverExpr); valueResolverExpr = resolvedValue; if (propertyMap.DestinationPropertyType != null) { var typePair = new TypePair(valueResolverExpr.Type, propertyMap.DestinationPropertyType); valueResolverExpr = MapExpression(typePair, valueResolverExpr, propertyMap, destValueExpr); } else { valueResolverExpr = SetMap(propertyMap, valueResolverExpr, destValueExpr); } ParameterExpression propertyValue; Expression setPropertyValue; if (valueResolverExpr == resolvedValue) { propertyValue = resolvedValue; setPropertyValue = setResolvedValue; } else { propertyValue = Variable(valueResolverExpr.Type, "propertyValue"); setPropertyValue = Assign(propertyValue, valueResolverExpr); } Expression mapperExpr; if (propertyMap.DestinationProperty is FieldInfo) { mapperExpr = propertyMap.SourceType != propertyMap.DestinationPropertyType ? Assign(destMember, ToType(propertyValue, propertyMap.DestinationPropertyType)) : Assign(getter, propertyValue); } else { var setter = ((PropertyInfo)propertyMap.DestinationProperty).GetSetMethod(true); if (setter == null) { mapperExpr = propertyValue; } else { mapperExpr = Assign(destMember, propertyMap.SourceType != propertyMap.DestinationPropertyType ? ToType(propertyValue, propertyMap.DestinationPropertyType) : propertyValue); } } mapperExpr = Block(new[] { setResolvedValue, setPropertyValue, mapperExpr }.Distinct()); if (propertyMap.PreCondition != null) { mapperExpr = IfThen( propertyMap.PreCondition.ConvertReplaceParameters(_source, _context), mapperExpr ); } if (propertyMap.Condition != null) { mapperExpr = IfThen( propertyMap.Condition.ConvertReplaceParameters( _source, _destination, ToType(propertyValue, propertyMap.Condition.Parameters[2].Type), ToType(getter, propertyMap.Condition.Parameters[2].Type), _context ), mapperExpr ); } return(Block(new[] { resolvedValue, propertyValue }.Distinct(), mapperExpr)); }
public TypePair GetAssociatedTypes(TypePair initialTypes) { return(new TypePair(Nullable.GetUnderlyingType(initialTypes.SourceType), initialTypes.DestinationType)); }
public bool IsMatch(TypePair context) => typeof(LambdaExpression).IsAssignableFrom(context.SourceType) && context.SourceType != typeof(LambdaExpression) && typeof(LambdaExpression).IsAssignableFrom(context.DestinationType) && context.DestinationType != typeof(LambdaExpression);
public bool IsMatch(TypePair context) => context.SourceType.IsNullableType();
public bool IsMatch(TypePair context) { return(context.DestinationType == typeof(string) && context.SourceType != typeof(string)); }
public MappingExpression(TypePair types, MemberList memberList) : base(memberList, types.SourceType, types.DestinationType) { }
protected override Mapper BuildCore(TypePair parentTypePair, MappingMember mappingMember) { return(BuildCore(mappingMember.TypePair)); }
protected override bool IsSupportedCore(TypePair typePair) { return(true); }
public bool IsMatch(TypePair context) { return(typeof(StringDictionary).IsAssignableFrom(context.SourceType)); }
public abstract bool IsMatch(TypePair context);
protected MappingExpressionBase(MemberList memberList, TypePair types) { Types = types; IsOpenGeneric = types.SourceType.IsGenericTypeDefinition || types.DestinationType.IsGenericTypeDefinition; TypeMapActions.Add(tm => tm.ConfiguredMemberList = memberList); }
public bool IsMatch(TypePair context) { return(context.DestinationType.IsNullableType()); }
public bool IsMatch(TypePair typePair) { return(typePair.SourceType.IsEnumerableType() && typePair.DestinationType.IsCollectionType()); }
private static Expression CreatePropertyMapFunc( PropertyMap propertyMap, IConfigurationProvider configurationProvider, TypeMapRegistry typeMapRegistry, ParameterExpression srcParam, ParameterExpression destParam, ParameterExpression ctxtParam, ref ParameterExpression propertyContext) { var valueResolverExpr = BuildValueResolverFunc(propertyMap, typeMapRegistry, srcParam, ctxtParam); var destMember = MakeMemberAccess(destParam, propertyMap.DestinationProperty.MemberInfo); Expression getter; var pi = propertyMap.DestinationProperty.MemberInfo as PropertyInfo; if (pi != null && pi.GetGetMethod(true) == null) { getter = Default(propertyMap.DestinationPropertyType); } else { getter = destMember; } var destValueExpr = propertyMap.UseDestinationValue ? getter : Default(propertyMap.TypeMap.DestinationType); if (propertyMap.SourceType != null && propertyMap.DestinationPropertyType != null) { var typePair = new TypePair(propertyMap.SourceType, propertyMap.DestinationPropertyType); var typeMap = configurationProvider.ResolveTypeMap(typePair); if (typeMap != null && (typeMap.TypeConverterType != null || typeMap.CustomMapper != null)) { if (!typeMap.Sealed) { typeMap.Seal(typeMapRegistry, configurationProvider); } valueResolverExpr = typeMap.MapExpression.ReplaceParameters(valueResolverExpr, destValueExpr, ctxtParam); } else { var match = configurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(typePair)); var expressionMapper = match as IObjectMapExpression; if (expressionMapper != null) { valueResolverExpr = expressionMapper.MapExpression(typeMapRegistry, configurationProvider, valueResolverExpr, destValueExpr, ctxtParam); } else { valueResolverExpr = SetMap(propertyMap, valueResolverExpr, destValueExpr, ref propertyContext); } } } else { valueResolverExpr = SetMap(propertyMap, valueResolverExpr, destValueExpr, ref propertyContext); } if (propertyMap.Condition != null) { valueResolverExpr = Condition( Invoke( propertyMap.Condition, srcParam, destParam, ToType(valueResolverExpr, propertyMap.Condition.Parameters[2].Type), ToType(destValueExpr, propertyMap.Condition.Parameters[2].Type), ctxtParam ), Convert(valueResolverExpr, propertyMap.DestinationPropertyType), destValueExpr ); } Expression mapperExpr; if (propertyMap.DestinationProperty.MemberInfo is FieldInfo) { mapperExpr = propertyMap.SourceType != propertyMap.DestinationPropertyType ? Assign(destMember, Convert(valueResolverExpr, propertyMap.DestinationPropertyType)) : Assign(getter, valueResolverExpr); } else { var setter = ((PropertyInfo)propertyMap.DestinationProperty.MemberInfo).GetSetMethod(true); if (setter == null) { mapperExpr = valueResolverExpr; } else { mapperExpr = Assign(destMember, propertyMap.SourceType != propertyMap.DestinationPropertyType ? Convert(valueResolverExpr, propertyMap.DestinationPropertyType) : valueResolverExpr); } } if (propertyMap.PreCondition != null) { mapperExpr = IfThen( Invoke(propertyMap.PreCondition, srcParam, ctxtParam), mapperExpr ); } return(mapperExpr); }
public MappingExpression(TypePair types, MemberList memberList) : base(memberList, types) { }
private void EmitEnumerableToEnumerable(Type parentType, TypeBuilder typeBuilder, TypePair typePair) { EmitEnumerableToTarget(parentType, typeBuilder, typePair, EnumerableToListMethod, EnumerableToListTemplateMethod); }
private static Expression ContextMap(TypePair typePair, Expression sourceParameter, Expression contextParameter, Expression destinationParameter) { var mapMethod = typeof(ResolutionContext).GetDeclaredMethods().First(m => m.Name == "Map").MakeGenericMethod(typePair.SourceType, typePair.DestinationType); return(Call(contextParameter, mapMethod, sourceParameter, destinationParameter)); }