简体   繁体   English

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

[英]Automapper Mapping, inheritance from generic list

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

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

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? 有没有办法使它正确?

Add this to your configuration 将此添加到您的配置

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

Basically Automapper wouldn't know how to map CabinCollection object 基本上, CabinCollection不知道如何映射CabinCollection对象

.Net fiddle link to show this .net小提琴链接以显示此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM