Automapper Mapping, inheritance from generic list

江枫思渺然 提交于 2021-01-28 11:40:53

问题


    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?


回答1:


Add this to your configuration

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

Basically Automapper wouldn't know how to map CabinCollection object

.Net fiddle link to show this



来源:https://stackoverflow.com/questions/44015032/automapper-mapping-inheritance-from-generic-list

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