Automapper: Map to Protected Property

喜你入骨 提交于 2019-12-05 02:48:05

Easiest way: Use AfterMap/BeforeMap constructs.

Mapper.CreateMap<PolicyDetail, Policy>()    
.AfterMap((src, dest) => dest.SetBilling(src.Billing));

https://github.com/AutoMapper/AutoMapper/wiki/Before-and-after-map-actions

Jimmy Bogard

Also possible: tell AutoMapper to recognize protected members:

Mapper.Initialize(cfg =>
{
    // map properties with public or internal getters
    cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
    cfg.CreateMap<Source, Destination>();
});

No extra AfterMap needed. AutoMapper by default looks for public properties, you have to tell it on a global or Profile basis to do something differently (https://github.com/AutoMapper/AutoMapper/wiki/Configuration#configuring-visibility)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!