string IMappingEngineRunner.FormatValue(ResolutionContext context) { TypeMap contextTypeMap = context.GetContextTypeMap(); IFormatterConfiguration configuration = contextTypeMap != null ? ConfigurationProvider.GetProfileConfiguration(contextTypeMap.Profile) : ConfigurationProvider.GetProfileConfiguration(ConfigurationStore.DefaultProfileName); object valueToFormat = context.SourceValue; string formattedValue = context.SourceValue.ToNullSafeString(); var formatters = configuration.GetFormattersToApply(context); foreach (var valueFormatter in formatters) { formattedValue = valueFormatter.FormatValue(context.CreateValueContext(valueToFormat)); valueToFormat = formattedValue; } if (formattedValue == null && !((IMappingEngineRunner)this).ShouldMapSourceValueAsNull(context)) { return(string.Empty); } return(formattedValue); }
private void DryRunTypeMap(ICollection <TypeMap> typeMapsChecked, ResolutionContext context) { if (context.TypeMap != null) { typeMapsChecked.Add(context.TypeMap); } var mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(context)); if (mapperToUse == null && context.SourceType.IsNullableType()) { var nullableContext = context.CreateValueContext(null, Nullable.GetUnderlyingType(context.SourceType)); mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableContext)); } if (mapperToUse == null) { throw new MapperConfigurationException(context); } if (mapperToUse is TypeMapMapper) { foreach (var propertyMap in context.TypeMap.GetPropertyMaps()) { if (!propertyMap.IsIgnored()) { var lastResolver = propertyMap.GetSourceValueResolvers().OfType <IMemberResolver>().LastOrDefault(); if (lastResolver != null) { var sourceType = lastResolver.MemberType; var destinationType = propertyMap.DestinationProperty.MemberType; var memberTypeMap = ((IConfigurationProvider)this).FindTypeMapFor(null, sourceType, destinationType); if (typeMapsChecked.Any(typeMap => Equals(typeMap, memberTypeMap))) { continue; } var memberContext = context.CreateMemberContext(memberTypeMap, null, null, sourceType, propertyMap); DryRunTypeMap(typeMapsChecked, memberContext); } } } } else if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper) { Type sourceElementType = TypeHelper.GetElementType(context.SourceType); Type destElementType = TypeHelper.GetElementType(context.DestinationType); TypeMap itemTypeMap = ((IConfigurationProvider)this).FindTypeMapFor(null, sourceElementType, destElementType); if (typeMapsChecked.Any(typeMap => Equals(typeMap, itemTypeMap))) { return; } var memberContext = context.CreateElementContext(itemTypeMap, null, sourceElementType, destElementType, 0); DryRunTypeMap(typeMapsChecked, memberContext); } }