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

我有很多类要像这样从 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

reactjs - `react-dom` 是库还是框架?

spring-boot - 将实体与 OneToMany 和 HATEOAS RessourceAs

haskell - Cabal/Stack 忽略自定义安装脚本的 ghc-options

azure - 服务主体帐户本地 AD Connect 同步