简体   繁体   English

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

[英]Automapper: use default mapping for all other members

I have a lot of classes to map from IDataReader like this:我有很多来自 IDataReader 的 map 类,如下所示:

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/*)
}

So i'd like to perform custom convertion on selected fields and do default mapping without explicitly coding.所以我想对选定的字段执行自定义转换,并在不显式编码的情况下进行默认映射。

Question looks very common, but I didn't find how to achieve this.问题看起来很常见,但我没有找到如何实现这一点。

So the most straightforward way is to write like this:所以最直接的方式就是这样写:

.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))

But there are some caveats.但有一些警告。 For example例如

.IncludeBase<IDataReader, Base>()
.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))

Here ForAllOtherMembers will override your base class definition.此处 ForAllOtherMembers 将覆盖您的基本 class 定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM