Automapper:对所有其他成员使用默认映射

问题描述 投票:0回答:1

我有很多这样的类可以从IDataReader进行映射:

class Dest
{
   public string Field1;
   public string Field2;
   public string Field3;
}

...

public DestProfile
{
   CreateMap<IDataReader, Dest>()
     .ForMember(d => d.Field1, opt => opt.MapFrom(/*Do some custom convertion here*/)
     .ForAllOtherMembers(/*Do default convention-based mapping for all other members/*)
}

因此,我想对选定字段执行自定义转换,并进行默认映射而无需显式编码。

问题看起来很普遍,但我没有找到实现该目标的方法。

c# .net automapper
1个回答
0
投票

所以最直接的方法是这样写:

.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))
© www.soinside.com 2019 - 2024. All rights reserved.