1

I want to create an Automapper extension method that performs a custom mapping on all properties of a certain type. Here is some pseudocode demonstrating what I want:

public static IMappingExpression<TSource, TDestination> UnflattenMembers<TSource, TDestination>
        (this IMappingExpression<TSource, TDestination> map)
    {
        foreach (property of the TSource Class)
        {
            if (property is of Type SpecialClass)
            {
                map.ForMember(dest => dest.property, opt => opt.MapFrom(src => src));
            }
        }
        return map;
    }

I want to do this to achieve a generic implementation of sydneyos's answer to this question: Using AutoMapper to unflatten a DTO.

E.g., Instead of doing this:

Mapper.CreateMap<SourceClass, DestClass>()
    .ForMember(dest => dest.Address, opt => opt.MapFrom( src => src )))
    .ForMember(dest => dest.MailingInfo, opt => opt.MapFrom( src => src )))
    .ForMember(dest => dest.Calendar, opt => opt.MapFrom( src => src )))
    etc...;

I want to to this:

Mapper.CreateMap<SourceClass, DestClass>().UnflattenMembers();
Community
  • 1
  • 1
asemahle
  • 20,235
  • 4
  • 40
  • 38

0 Answers0