我需要使用Automapper映射到类的protected属性.我在此类上公开了一个public方法,该方法用于为属性设置值.此方法需要parameter.如何将值映射到此类?
I need to map to a protected property on a class using Automapper. I've got a public method exposed on this class that is used to set values to the property. This method requires a parameter. How can I map a value to this class?
目的地类别:
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"); } } }这是我的Automapper代码(伪代码)的样子:
Here's what my Automapper code (pseudo code) looks like:
Mapper.CreateMap<PolicyDetail, Policy>() .ForMember(d => d.SetBilling(???), s => s.MapFrom(x => x.Billing));我需要将Billing类传递给SetBilling(Billing billing)方法.我该怎么做呢?或者,我可以只设置受保护的结算属性吗?
I need to pass a Billing class to the SetBilling(Billing billing) method. How do I do this? Or, can I just set the protected Billing property?
推荐答案最简单的方法:使用AfterMap/BeforeMap构造.
Easiest way: Use AfterMap/BeforeMap constructs.
Mapper.CreateMap<PolicyDetail, Policy>() .AfterMap((src, dest) => dest.SetBilling(src.Billing));github/AutoMapper/AutoMapper/wiki/Before-and -after-map-actions
更多推荐
自动映射器:映射到受保护的属性
发布评论