Example #1
0
        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 (AutoMapperMappingException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new AutoMapperMappingException(newContext, ex);
            }
        }
Example #2
0
        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 && PrimitiveExtensions.IsNullableType(context.SourceType))
            {
                var nullableContext = context.CreateValueContext(null, Nullable.GetUnderlyingType(context.SourceType));

                mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableContext));
            }

            if (mapperToUse == null)
            {
                throw new AutoMapperConfigurationException(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).ResolveTypeMap(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)
            {
                var sourceElementType = TypeHelper.GetElementType(context.SourceType);
                var destElementType   = TypeHelper.GetElementType(context.DestinationType);
                var itemTypeMap       = ((IConfigurationProvider)this).ResolveTypeMap(sourceElementType, destElementType);

                if (typeMapsChecked.Any(typeMap => Equals(typeMap, itemTypeMap)))
                {
                    return;
                }

                var memberContext = context.CreateElementContext(itemTypeMap, null, sourceElementType, destElementType,
                                                                 0);

                DryRunTypeMap(typeMapsChecked, memberContext);
            }
        }