我有很多类要像这样从 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 将覆盖您的基类定义。
https://stackoverflow.com/questions/58237173/
相关文章:
python - 如何使用 Pandas 计算数据框中的类标签频率?
reactjs - react native : Module RCTLog is not a re
asynchronous - 如何使 promise 因 clojure 错误而失败?
ruby-on-rails - 如何使用 Vue 前端和 Rails 后端实现 env 变量?是否需
oauth-2.0 - 松弛 Oauth : Automatically authorize use
cmake - 安装问题: Cmake error : include could not find
spring-boot - 将实体与 OneToMany 和 HATEOAS RessourceAs