自动映射,从通用列表继承

时间:2017-05-17 03:29:33

标签: c# automapper

    public class Flight {
        public CabinCollection Cabins { get; set; }
    }

    public class CabinCollection : List<Cabin>
    {
        public Cabin Lowest { set; get; }
    }

source和dest类具有相同的成员

   1)  Mapper.Initialize(cfg => {
        cfg.CreateMap<Domain.Flight, Contract.Flight>();
        cfg.CreateMap<Domain.Cabin, Contract.Cabin>();
    });

    List<Flight> res = Mapper.Map<List<Flight>>(flights);

它有效,但会员最低&#39;为空

   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>>()
        });

它有效且会员最低&#39;已映射,但列表为空

有没有办法让它正确?

1 个答案:

答案 0 :(得分:0)

将此添加到您的配置

cfg.CreateMap<Domain.CabinCollection, Contract.CabinCollectionDest>();

基本上,Automapper不知道如何映射CabinCollection对象

.Net fiddle link to show this