【问题标题】:Private/Internal not Mapping私有/内部不映射
【发布时间】: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


    【解决方案1】:

    如果您使用 AutoMapper,则必须将初始化程序中的 ShouldMapProperty 设置为适当的值。

    这样做是这样的:

    Mapper.Initialize(cfg =>
    {
         cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
         cfg.CreateMap<Source, Destination>();
    });
    

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多