下面是现在的样子。DestinationA和DestinationB是从某些DestinationBase类派生出来的。我需要忽略所有这些派生类的一些公共属性。全局是否可以应用这些忽略选项,而不必对所有派生目标类重复使用?
Mapper.CreateMap<SourceA, DestinationA>()
.ForMember(d => d.PropA, opt => opt.Ignore())
.ForMember(d => d.PropB, opt => opt.Ignore())
.ForMember(d => d.PropC, opt => opt.Ignore());
Mapper.CreateMap<SourceB, DestinationB>()
.ForMember(d => d.PropA, opt => opt.Ignore())
.ForMember(d => d.PropB, opt => opt.Ignore())
.ForMember(d => d.PropC, opt => opt.Ignore());
我期待着这样的事情:
Mapper.CreateMap<DestinationBase>().ForAllSource()
.ForMember(d => d.PropA, opt => opt.Ignore())
.ForMember(d => d.PropB, opt => opt.Ignore())
.ForMember(d => d.PropC, opt => opt.Ignore());
发布于 2017-10-08 22:22:58
您可以忽略所有未映射的全局属性。虽然这与automapper的主要优点相矛盾,但只允许进行显式映射:这是Automapper 6的:
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MyProfile());
// ignore all unmapped properties globally
cfg.ForAllMaps((map, exp) => exp.ForAllOtherMembers(opt => opt.Ignore()));
});
https://stackoverflow.com/questions/8663771
复制相似问题