这是现在的样子。 DestinationA 和 DestinationB 派生自某个 DestinationBase 类。我需要忽略所有这些派生类的一些常见属性。无论如何都可以应用这些忽略选项globally无需对所有派生目标类重复?
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());
您可以全局忽略所有未映射的属性。
尽管这与自动映射器的主要优点相矛盾,但仅允许进行显式映射:
这是针对 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()));
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)