本文介绍了自动映射器映射,从通用列表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class Flight {
public CabinCollection Cabins { get; set; }
}
public class CabinCollection : List<Cabin>
{
public Cabin Lowest { set; get; }
}
source和dest类具有相同的成员
source and dest class have the same members
1) Mapper.Initialize(cfg => { cfg.CreateMap<Domain.Flight, Contract.Flight>(); cfg.CreateMap<Domain.Cabin, Contract.Cabin>(); }); List<Flight> res = Mapper.Map<List<Flight>>(flights);它可以工作,但成员最低"为空
It works but the member 'lowest' is null
2) Mapper.Initialize(cfg => { cfg.CreateMap<Domain.Flight, Contract.Flight>(); cfg.CreateMap<Domain.Cabin, Contract.Cabin>(); cfg.CreateMap<Domain.CabinCollection,Contract.CabinColection> .IncludeBase<List<Domain.Cabin>, List<Contract.Cabin>>() });它可以工作并且成员最低"被映射,但是列表为空
It works and the member 'lowest' mapped, but the list is null
有没有办法做到这一点?
Is there a way make it right?
推荐答案将此添加到您的配置中
cfg.CreateMap<Domain.CabinCollection, Contract.CabinCollectionDest>();基本上,Automapper不知道如何映射 CabinCollection 对象
Basically Automapper wouldn't know how to map CabinCollection object
.Net小提琴链接以显示此内容
更多推荐
自动映射器映射,从通用列表继承
发布评论