自动映射器:映射到受保护的属性 [英] Automapper: Map to Protected Property
问题描述
我需要使用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));
https://github.com/AutoMapper/AutoMapper/wiki/Before-and -after-map-actions
这篇关于自动映射器:映射到受保护的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!