2016-11-28 62 views
0

我有一個奇怪的問題,我從來沒有遇到過。Private/Internal not Mapping

我有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; } 
} 

我定義的映射在我的統一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); 

奇怪的是...我可以檢查父類和所有我已初步傳遞到我的模型(父類)是否有值的值。但是,當它映射到新對象(childClass) - 公司名稱...時,_clientType和Client_Type_爲空(最終影響Client_Type)。

關於我在做什麼的任何建議是錯誤的?

回答

1

如果您使用AutoMapper,則必須將初始值設定項中的ShouldMapProperty設置爲適當的值。

這是這樣完成的:

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

這就是它 - 太感謝你了! – MadScout