public object Map(ResolutionContext context, IMappingEngineRunner mapper) { if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } var sourceEnumerableValue = (IEnumerable)context.SourceValue ?? new object[0]; IEnumerable <object> keyValuePairs = sourceEnumerableValue.Cast <object>(); Type genericSourceDictType = context.SourceType.GetDictionaryType(); Type sourceKeyType = genericSourceDictType.GetGenericArguments()[0]; Type sourceValueType = genericSourceDictType.GetGenericArguments()[1]; Type sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType); Type genericDestDictType = context.DestinationType.GetDictionaryType(); Type destKeyType = genericDestDictType.GetGenericArguments()[0]; Type destValueType = genericDestDictType.GetGenericArguments()[1]; var dictionaryEntries = keyValuePairs.OfType <DictionaryEntry>(); if (dictionaryEntries.Any()) { keyValuePairs = dictionaryEntries.Select(e => Activator.CreateInstance(sourceKvpType, e.Key, e.Value)); } object destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType); int count = 0; foreach (object keyValuePair in keyValuePairs) { object sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]); object sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]); TypeMap keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType, destKeyType); TypeMap valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType, destValueType); ResolutionContext keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType, destKeyType, count); ResolutionContext valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType, destValueType, count); object destKey = mapper.Map(keyContext); object destValue = mapper.Map(valueContext); genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] { destKey, destValue }); count++; } return(destDictionary); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context)) return null; var sourceEnumerableValue = (IEnumerable) context.SourceValue ?? new object[0]; IEnumerable<object> keyValuePairs = sourceEnumerableValue.Cast<object>(); Type genericSourceDictType = context.SourceType.GetDictionaryType(); Type sourceKeyType = genericSourceDictType.GetGenericArguments()[0]; Type sourceValueType = genericSourceDictType.GetGenericArguments()[1]; Type sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType); Type genericDestDictType = context.DestinationType.GetDictionaryType(); Type destKeyType = genericDestDictType.GetGenericArguments()[0]; Type destValueType = genericDestDictType.GetGenericArguments()[1]; var dictionaryEntries = keyValuePairs.OfType<DictionaryEntry>(); if (dictionaryEntries.Any()) keyValuePairs = dictionaryEntries.Select(e => Activator.CreateInstance(sourceKvpType, e.Key, e.Value)); object destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType); int count = 0; foreach (object keyValuePair in keyValuePairs) { object sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]); object sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]); TypeMap keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType, destKeyType); TypeMap valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType, destValueType); ResolutionContext keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType, destKeyType, count); ResolutionContext valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType, destValueType, count); object destKey = mapper.Map(keyContext); object destValue = mapper.Map(valueContext); genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue}); count++; } return destDictionary; }
public object ResolveValue(ResolutionContext context, IMappingEngineRunner mappingEngine) { var ctorArgs = new List <object>(); foreach (var map in CtorParams) { var result = map.ResolveValue(context); var sourceType = result.Type; var destinationType = map.Parameter.ParameterType; var typeMap = mappingEngine.ConfigurationProvider.ResolveTypeMap(result, destinationType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType; var newContext = context.CreateTypeContext(typeMap, result.Value, null, targetSourceType, destinationType); if (typeMap == null && map.Parameter.IsOptional) { object value = map.Parameter.DefaultValue; ctorArgs.Add(value); } else { var value = mappingEngine.Map(newContext); ctorArgs.Add(value); } } return(_runtimeCtor.Value(ctorArgs.ToArray())); }
private static void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (!propertyMap.CanResolveValue()) { return; } var result = propertyMap.ResolveValue(context); var newContext = context.CreateMemberContext(null, result.Value, null, result.Type, propertyMap); if (!propertyMap.ShouldAssignValue(newContext)) { return; } try { var propertyValueToAssign = mapper.Map(newContext); if (propertyMap.CanBeSet) { propertyMap.DestinationProperty.SetValue(mappedObject, propertyValueToAssign); } } catch (MapperMappingException) { throw; } catch (Exception ex) { throw new MapperMappingException(newContext, ex); } }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var newSource = context.TypeMap.Substitution(context.SourceValue); var substitutionContext = context.CreateValueContext(newSource, newSource.GetType()); return(mapper.Map(substitutionContext)); }
public object ResolveValue(ResolutionContext context, IMappingEngineRunner mappingEngine) { var ctorArgs = new List<object>(); foreach (var map in CtorParams) { var result = map.ResolveValue(context); var sourceType = result.Type; var destinationType = map.Parameter.ParameterType; var typeMap = mappingEngine.ConfigurationProvider.ResolveTypeMap(result, destinationType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType; var newContext = context.CreateTypeContext(typeMap, result.Value, null, targetSourceType, destinationType); if (typeMap == null && map.Parameter.IsOptional) { object value = map.Parameter.DefaultValue; ctorArgs.Add(value); } else { var value = mappingEngine.Map(newContext); ctorArgs.Add(value); } } return _runtimeCtor.Value(ctorArgs.ToArray()); }
private void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (propertyMap.CanResolveValue()) { ResolutionResult result; try { result = propertyMap.ResolveValue(context); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { var errorContext = CreateErrorContext(context, propertyMap, null); throw new AutoMapperMappingException(errorContext, ex); } if (result.ShouldIgnore) { return; } object destinationValue = propertyMap.DestinationProperty.GetValue(mappedObject); var sourceType = result.Type; var destinationType = propertyMap.DestinationProperty.MemberType; var typeMap = mapper.ConfigurationProvider.FindTypeMapFor(result, destinationType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType; var newContext = context.CreateMemberContext(typeMap, result.Value, destinationValue, targetSourceType, propertyMap); if (!propertyMap.ShouldAssignValue(newContext)) { return; } try { object propertyValueToAssign = mapper.Map(newContext); AssignValue(propertyMap, mappedObject, propertyValueToAssign); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { throw new AutoMapperMappingException(newContext, ex); } } }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var typeMap = mapper.ConfigurationProvider.FindClosedGenericTypeMapFor(context); var newContext = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType); return(mapper.Map(newContext)); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var typeMap = mapper.ConfigurationProvider.FindClosedGenericTypeMapFor(context); var newContext = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType); return mapper.Map(newContext); }
public TDest Map <TDest>(object source) { TypeMap typeMap = _mapper.ConfigurationProvider.FindTypeMapFor(source, default(TDest), source.GetType(), typeof(TDest)); var mappingOperationOptions = new MappingOperationOptions(); var resolutionContext = new ResolutionContext(typeMap, source, source.GetType(), typeof(TDest), mappingOperationOptions, Mapper.Engine);// IMappingEngine engine return((TDest)_mapper.Map(resolutionContext)); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var source = context.SourceValue; var sourceType = source.GetType(); var sourceTypeDetails = new TypeDetails(sourceType, _ => true, _ => true); var membersDictionary = sourceTypeDetails.PublicReadAccessors.ToDictionary(p => p.Name, p => p.GetMemberValue(source)); var newContext = context.CreateTypeContext(null, membersDictionary, context.DestinationValue, membersDictionary.GetType(), context.DestinationType); return mapper.Map(newContext); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } Type genericSourceDictType = context.SourceType.GetDictionaryType(); Type sourceKeyType = genericSourceDictType.GetGenericArguments()[0]; Type sourceValueType = genericSourceDictType.GetGenericArguments()[1]; Type sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType); Type genericDestDictType = context.DestinationType.GetDictionaryType(); Type destKeyType = genericDestDictType.GetGenericArguments()[0]; Type destValueType = genericDestDictType.GetGenericArguments()[1]; var kvpEnumerator = GetKeyValuePairEnumerator(context, sourceKvpType); var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType); int count = 0; while (kvpEnumerator.MoveNext()) { var keyValuePair = kvpEnumerator.Current; object sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]); object sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]); TypeMap keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType, destKeyType); TypeMap valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType, destValueType); ResolutionContext keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType, destKeyType, count); ResolutionContext valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType, destValueType, count); object destKey = mapper.Map(keyContext); object destValue = mapper.Map(valueContext); genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] { destKey, destValue }); count++; } return(destDictionary); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var newSource = context.TypeMap.Substitution(context.SourceValue); var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(newSource.GetType(), context.DestinationType); var substitutionContext = context.CreateTypeContext(typeMap, newSource, context.DestinationValue, newSource.GetType(), context.DestinationType); return(mapper.Map(substitutionContext)); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context)) { return null; } var genericSourceDictType = PrimitiveExtensions.GetDictionaryType(context.SourceType); var sourceKeyType = genericSourceDictType.GetGenericArguments()[0]; var sourceValueType = genericSourceDictType.GetGenericArguments()[1]; var sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType); var genericDestDictType = PrimitiveExtensions.GetDictionaryType(context.DestinationType); var destKeyType = genericDestDictType.GetGenericArguments()[0]; var destValueType = genericDestDictType.GetGenericArguments()[1]; var kvpEnumerator = GetKeyValuePairEnumerator(context, sourceKvpType); var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType); var count = 0; while (kvpEnumerator.MoveNext()) { var keyValuePair = kvpEnumerator.Current; var sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]); var sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]); var keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType, destKeyType); var valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType, destValueType); var keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType, destKeyType, count); var valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType, destValueType, count); var destKey = mapper.Map(keyContext); var destValue = mapper.Map(valueContext); genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue}); count++; } return destDictionary; }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var newSource = context.TypeMap.Substitution(context.SourceValue); var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(newSource.GetType(), context.DestinationType); var substitutionContext = context.CreateTypeContext(typeMap, newSource, context.DestinationValue, newSource.GetType(), context.DestinationType); return mapper.Map(substitutionContext); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var source = context.SourceValue; var sourceType = source.GetType(); var sourceTypeDetails = new TypeDetails(sourceType, _ => true, _ => true); var membersDictionary = sourceTypeDetails.PublicReadAccessors.ToDictionary(p => p.Name, p => p.GetMemberValue(source)); var newContext = context.CreateTypeContext(null, membersDictionary, context.DestinationValue, membersDictionary.GetType(), context.DestinationType); return(mapper.Map(newContext)); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var container = (ILifetimeScope)context.Options.ServiceCtor(typeof(ILifetimeScope)); var cacheManager = container.Resolve <ICacheManager>(TypedParameter.From(GetType())); // create destination value by passing source value as IPagedList var createDestination = cacheManager.Get($"lambda-{GetType().Name}-create_{context.DestinationType.FullName}", acquireContext => { var constructorInfo = context.DestinationType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new[] { typeof(IPagedList) }, new ParameterModifier[0]); var parameter = Expression.Parameter(typeof(object)); var parameterConvert = Expression.Convert(parameter, typeof(IPagedList)); var @new = Expression.New(constructorInfo, parameterConvert); return(Expression.Lambda <Func <object, object> >(@new, parameter).Compile()); }); var destinationValue = createDestination(context.SourceValue); // mapping data object var sourceItemType = context.SourceType.GenericTypeArguments[0]; var destinationItemType = context.DestinationType.GenericTypeArguments[0]; var enumerableSourceItemType = typeof(IEnumerable <>).MakeGenericType(sourceItemType); var enumerableDestinationItemType = typeof(IEnumerable <>).MakeGenericType(destinationItemType); var typeContext = context.CreateTypeContext(null, context.SourceValue, null, enumerableSourceItemType, enumerableDestinationItemType); var data = mapper.Map(typeContext); // set data: using expression tree instead of reflection, and have the compiled lambda cached (much better performance) var setData = cacheManager.Get($"lambda-{GetType().Name}-set_data-from_{context.SourceType.FullName}-to_{context.DestinationType.FullName}", acquireContext => { var instance = Expression.Parameter(typeof(object), "model"); var instanceConvert = Expression.Convert(instance, context.DestinationType); var value = Expression.Parameter(typeof(object), "data"); var valueConvert = Expression.Convert(value, enumerableDestinationItemType); var property = Expression.Property(instanceConvert, "Data"); var setMethod = ((PropertyInfo)property.Member).GetSetMethod(); var call = Expression.Call(instanceConvert, setMethod, valueConvert); return(Expression.Lambda <Action <object, object> >(call, instance, value).Compile()); }); setData(destinationValue, data); // set the value // reflection way to set data // var dataProp = context.DestinationType.GetProperty("Data"); // dataProp.SetValue(destinationValue, data); return(destinationValue); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } ICollection <object> enumerableValue = ((IEnumerable)context.SourceValue ?? new object[0]) .Cast <object>() .ToList(); Type sourceElementType = TypeHelper.GetElementType(context.SourceType, enumerableValue); Type destElementType = TypeHelper.GetElementType(context.DestinationType); var sourceLength = enumerableValue.Count; var destination = GetOrCreateDestinationObject(context, mapper, destElementType, sourceLength); var enumerable = GetEnumerableFor(destination); ClearEnumerable(enumerable); int i = 0; foreach (object item in enumerableValue) { var newContext = context.CreateElementContext(null, item, sourceElementType, destElementType, i); var elementResolutionResult = new ResolutionResult(newContext); var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(elementResolutionResult, destElementType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceElementType; Type targetDestinationType = typeMap != null ? typeMap.DestinationType : destElementType; newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, i); object mappedValue = mapper.Map(newContext); SetElementValue(enumerable, mappedValue, i); i++; } object valueToAssign = destination; return(valueToAssign); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var sourceValue = (IEnumerable)context.SourceValue ?? new object[0]; IEnumerable <object> enumerableValue = sourceValue.Cast <object>(); Type sourceElementType = TypeHelper.GetElementType(context.SourceType, sourceValue); Type destElementType = TypeHelper.GetElementType(context.DestinationType); var sourceLength = enumerableValue.Count(); var destination = (context.DestinationValue ?? CreateDestinationObject(context, destElementType, sourceLength, mapper)); var enumerable = GetEnumerableFor(destination); ClearEnumerable(enumerable); int i = 0; foreach (object item in enumerableValue) { var newContext = context.CreateElementContext(null, item, sourceElementType, destElementType, i); var elementResolutionResult = new ResolutionResult(newContext); var typeMap = mapper.ConfigurationProvider.FindTypeMapFor(elementResolutionResult, destElementType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceElementType; Type targetDestinationType = typeMap != null ? typeMap.DestinationType : destElementType; newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, i); object mappedValue = mapper.Map(newContext); SetElementValue(enumerable, mappedValue, i); i++; } object valueToAssign = destination; return(valueToAssign); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var sourceEnumerableValue = (IEnumerable) context.SourceValue ?? new object[0]; var enumerableValue = sourceEnumerableValue.Cast<object>(); var sourceElementType = TypeHelper.GetElementType(context.SourceType, sourceEnumerableValue); var genericDestDictType = PrimitiveExtensions.GetDictionaryType(context.DestinationType); var destKeyType = genericDestDictType.GetGenericArguments()[0]; var destValueType = genericDestDictType.GetGenericArguments()[1]; var destKvpType = KvpType.MakeGenericType(destKeyType, destValueType); var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType); var count = 0; foreach (var item in enumerableValue) { var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(item, null, sourceElementType, destKvpType); var targetSourceType = typeMap != null ? typeMap.SourceType : sourceElementType; var targetDestinationType = typeMap != null ? typeMap.DestinationType : destKvpType; var newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, count); var mappedValue = mapper.Map(newContext); var keyProperty = mappedValue.GetType().GetProperty("Key"); var destKey = keyProperty.GetValue(mappedValue, null); var valueProperty = mappedValue.GetType().GetProperty("Value"); var destValue = valueProperty.GetValue(mappedValue, null); genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue}); count++; } return destDictionary; }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var sourceEnumerableValue = (IEnumerable)context.SourceValue ?? new object[0]; var enumerableValue = sourceEnumerableValue.Cast <object>(); var sourceElementType = TypeHelper.GetElementType(context.SourceType, sourceEnumerableValue); var genericDestDictType = PrimitiveExtensions.GetDictionaryType(context.DestinationType); var destKeyType = genericDestDictType.GetGenericArguments()[0]; var destValueType = genericDestDictType.GetGenericArguments()[1]; var destKvpType = KvpType.MakeGenericType(destKeyType, destValueType); var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType); var count = 0; foreach (var item in enumerableValue) { var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(item, null, sourceElementType, destKvpType); var targetSourceType = typeMap != null ? typeMap.SourceType : sourceElementType; var targetDestinationType = typeMap != null ? typeMap.DestinationType : destKvpType; var newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, count); var mappedValue = mapper.Map(newContext); var keyProperty = mappedValue.GetType().GetProperty("Key"); var destKey = keyProperty.GetValue(mappedValue, null); var valueProperty = mappedValue.GetType().GetProperty("Value"); var destValue = valueProperty.GetValue(mappedValue, null); genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] { destKey, destValue }); count++; } return(destDictionary); }
private static void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (!propertyMap.CanResolveValue()) return; var result = propertyMap.ResolveValue(context); var newContext = context.CreateMemberContext(null, result.Value, null, result.Type, propertyMap); if (!propertyMap.ShouldAssignValue(newContext)) return; try { var propertyValueToAssign = mapper.Map(newContext); if (propertyMap.CanBeSet) { TypeConverter typeConverter = TypeDescriptor.GetConverter(propertyValueToAssign); Type sourcePropertyType = propertyValueToAssign.GetType(); Type destinationPropertyType = propertyMap.DestinationProperty.MemberType; if (typeConverter != null && sourcePropertyType != destinationPropertyType) { propertyValueToAssign = typeConverter.ConvertTo(propertyValueToAssign, destinationPropertyType); } propertyMap.DestinationProperty.SetValue(mappedObject, propertyValueToAssign); } } catch (Exception ex) { throw new AutoMapperMappingException(newContext, ex); } }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { var newSource = context.TypeMap.Substitution(context.SourceValue); var substitutionContext = context.CreateValueContext(newSource, newSource.GetType()); return mapper.Map(substitutionContext); }
private void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (propertyMap.CanResolveValue() && propertyMap.ShouldAssignValuePreResolving(context)) { ResolutionResult result; Exception resolvingExc = null; try { result = propertyMap.ResolveValue(context); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { var errorContext = CreateErrorContext(context, propertyMap, null); resolvingExc = new AutoMapperMappingException(errorContext, ex); result = new ResolutionResult(context); } if (result.ShouldIgnore) return; object destinationValue = propertyMap.GetDestinationValue(mappedObject); var sourceType = result.Type; var destinationType = propertyMap.DestinationProperty.MemberType; var typeMap = mapper.ConfigurationProvider.FindTypeMapFor(result, destinationType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType; var newContext = context.CreateMemberContext(typeMap, result.Value, destinationValue, targetSourceType, propertyMap); if (!propertyMap.ShouldAssignValue(newContext)) return; // If condition succeeded and resolving failed, throw if (resolvingExc != null) throw resolvingExc; try { object propertyValueToAssign = mapper.Map(newContext); AssignValue(propertyMap, mappedObject, propertyValueToAssign); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { throw new AutoMapperMappingException(newContext, ex); } } }
private void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (propertyMap.CanResolveValue()) { if (!propertyMap.ShouldAssignValue(context.CreateMemberContext(null, null, null, null, propertyMap))) return; object destinationValue = null; ResolutionResult result; try { result = propertyMap.ResolveValue(context); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { var errorContext = CreateErrorContext(context, propertyMap, destinationValue); throw new AutoMapperMappingException(errorContext, ex); } if (result.ShouldIgnore) return; if (propertyMap.UseDestinationValue) { destinationValue = propertyMap.DestinationProperty.GetValue(mappedObject); } var sourceType = result.Type; var destinationType = propertyMap.DestinationProperty.MemberType; var typeMap = mapper.ConfigurationProvider.FindTypeMapFor(result, destinationType); Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType; var newContext = context.CreateMemberContext(typeMap, result.Value, destinationValue, targetSourceType, propertyMap); try { object propertyValueToAssign = mapper.Map(newContext); AssignValue(propertyMap, mappedObject, propertyValueToAssign); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { throw new AutoMapperMappingException(newContext, ex); } } }
private static void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (!propertyMap.CanResolveValue()) return; var result = propertyMap.ResolveValue(context); var newContext = context.CreateMemberContext(null, result.Value, null, result.Type, propertyMap); if (!propertyMap.ShouldAssignValue(newContext)) return; try { var propertyValueToAssign = mapper.Map(newContext); if (propertyMap.CanBeSet) propertyMap.DestinationProperty.SetValue(mappedObject, propertyValueToAssign); } catch (Exception ex) { throw new AutoMapperMappingException(newContext, ex); } }
private void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap) { if (!propertyMap.CanResolveValue() || !propertyMap.ShouldAssignValuePreResolving(context)) { return; } ResolutionResult result; Exception resolvingExc = null; try { result = propertyMap.ResolveValue(context); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { var errorContext = CreateErrorContext(context, propertyMap, null); resolvingExc = new AutoMapperMappingException(errorContext, ex); result = new ResolutionResult(context); } if (result.ShouldIgnore) { return; } var destinationValue = propertyMap.GetDestinationValue(mappedObject); var sourceType = result.Type; var destinationType = propertyMap.DestinationProperty.MemberType; var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(result, destinationType); var targetSourceType = typeMap != null ? typeMap.SourceType : sourceType; var newContext = context.CreateMemberContext(typeMap, result.Value, destinationValue, targetSourceType, propertyMap); if (!propertyMap.ShouldAssignValue(newContext)) { return; } // If condition succeeded and resolving failed, throw if (resolvingExc != null) { throw resolvingExc; } try { var propertyValueToAssign = mapper.Map(newContext); AssignValue(propertyMap, mappedObject, propertyValueToAssign); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { throw new AutoMapperMappingException(newContext, ex); } }