我需要映射到protected
使用类的属性Automapper
。我有一个public
此类上公开的方法,用于设置属性的值。该方法需要一个parameter
。如何将值映射到此类?
目的地舱位:
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) 方法。我该怎么做呢?或者,我可以只设置受保护的 Billing 属性吗?
也可以:告诉 AutoMapper 识别受保护的成员:
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 默认情况下会查找公共属性,您必须在全局或配置文件的基础上告诉它做一些不同的事情(https://github.com/AutoMapper/AutoMapper/wiki/Configuration#configuring-visibility https://github.com/AutoMapper/AutoMapper/wiki/Configuration#configuring-visibility)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)