我有很多类要像这样从 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/*)
}
所以我想对选定的字段执行自定义转换,并在不显式编码的情况下进行默认映射。
问题看起来很常见,但我没有找到如何实现这一点。
最佳答案
所以最直接的方式就是这样写:
.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))
但是有一些注意事项。例如
.IncludeBase<IDataReader, Base>()
.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))
此处 ForAllOtherMembers 将覆盖您的基类定义。
关于c# - Automapper:对所有其他成员使用默认映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237173/