Automapper设置内部对象属性

时间:2017-08-29 13:27:37

标签: c# automapper

如何使用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();

1 个答案:

答案 0 :(得分:0)

您需要ForPath而不是ForMember。