我需要使用 Automapper
映射到类的 protected
属性。我在此类上公开了一个 public
方法,用于为属性设置值。此方法需要一个参数
。如何将值映射到此类?
目标类:
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
代码(伪代码)的样子:
Mapper.CreateMap<PolicyDetail, Policy>()
.ForMember(d => d.SetBilling(???),
s => s.MapFrom(x => x.Billing));
我需要将 Billing 类传递给 SetBilling(Billing billing) 方法。我该怎么做呢?或者,我可以只设置 protected Billing 属性吗?
最佳答案
也可能:告诉 AutoMapper 识别 protected 成员:
Mapper.Initialize(cfg =>
{
// map properties with public or internal getters
cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
cfg.CreateMap<Source, Destination>();
});
不需要额外的 AfterMap。 AutoMapper 默认查找公共(public)属性,您必须在全局或配置文件的基础上告诉它做一些不同的事情 (https://github.com/AutoMapper/AutoMapper/wiki/Configuration#configuring-visibility)
关于c# - Automapper:映射到 protected 属性(property),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568428/