如何创建Automapper扩展方法以对特定类型的所有属性执行映射?

时间:2015-10-01 13:38:35

标签: c# automapper

我想创建一个Automapper扩展方法,该方法对特定类型的所有属性执行自定义映射。这是一些伪代码,展示了我想要的东西:

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;
    }

我想这样做是为了实现sydneyos对这个问题的回答的通用实现:Using AutoMapper to unflatten a DTO

例如,而不是这样做:

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...;

我想这样:

Mapper.CreateMap<SourceClass, DestClass>().UnflattenMembers();

0 个答案:

没有答案