【发布时间】:2016-11-28 22:14:14
【问题描述】:
我有一个从未遇到过的奇怪问题。
我有 2 节课:
父类:
public class ParentClass
{
private int? _clientType;
internal int? Client_Type_
{
get { return _clientType; }
set { _clientType = value; }
}
public string Company_Name{ get; set; }
public string Client_Type
{
get { return MyDictionary[_clientType.GetValueOrDefault(0)];
}
}
儿童班:
public class ChildClass : ParentClass
{
public string Full_Name { get; set; }
}
我在我的 Unity IoC 容器中定义映射:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ParentClass, ChildClass>();
}
IMapper mapper = config.CreateMapper();
container.RegisterInstance(mapper);
在我的代码中,我是这样映射的:
var parentClass = GetParentClass();
var mappedClass = _mapper.Map<ChildClass>(parentClass);
奇怪的是……我可以检查 parentClass 的值,并且我最初传递给我的模型(parentClass)的所有值都在那里。但是,当它映射到新对象(childClass)时 - 公司名称仍然存在......但 _clientType 和 Client_Type_ 为空(最终会影响 Client_Type)。
关于我做错了什么有什么建议吗?
【问题讨论】:
标签: c# .net automapper