自动映射器映射,从通用列表继承 [英] Automapper Mapping, inheritance from generic list

查看:53
本文介绍了自动映射器映射,从通用列表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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小提琴链接以显示此内容

这篇关于自动映射器映射,从通用列表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆