我需要使用Automapper映射到类上的受保护属性.我有一个公共方法暴露在这个类,用于设置属性的值.此方法需要一个参数.如何将值映射到此类?
目的地类:
public class Policy { private Billing _billing; protected Billing Billing { get { return _billing; } set { _billing = value; } } public void SetBilling(Billing billing) { if (billing != null) { Billing = billing; } else { throw new NullReferenceException("Billing can't be null"); } } }
Mapper.CreateMap<PolicyDetail,Policy>() .ForMember(d => d.SetBilling(???),s => s.MapFrom(x => x.Billing));
解决方法
最简单的方法:使用AfterMap / BeforeMap构造.
Mapper.CreateMap<PolicyDetail,Policy>() .AfterMap((src,dest) => dest.SetBilling(src.Billing));
https://github.com/AutoMapper/AutoMapper/wiki/Before-and-after-map-actions