如何使用Automapper设置嵌套对象的内部属性? 股东具有类联系人的内在属性。 属性PhysicalOrPostalAddresses需要映射到Individual.Address或Corporate.Addresses。
var holderMap = CreateMap<Shareholder, Trustee>(MemberList.None);
holderMap
.ForMember(dest => dest.Individual, opt =>
{
opt.Condition(src => src.Person != null);
opt.MapFrom(src => src.Person);
})
.ForMember(dest => dest.Corporate, opt =>
{
opt.Condition(src => src.Organisation != null);
opt.MapFrom(src => src.Organisation);
})
// this code doesn't work
.ForMember(dest => dest.Corporate.Addresses, o =>
{
o.Condition(s => s.Organisation != null);
o.MapFrom(s => s.Contacts.PhysicalOrPostalAddresses);
})
.ForMember(dest => dest.Individual.Addresses, o =>
{
o.Condition(s => s.Person != null);
o.MapFrom(s => s.Contacts.PhysicalOrPostalAddresses);
})
.ReverseMap();
答案 0 :(得分:0)
您需要ForPath而不是ForMember。