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

编程入门 行业动态 更新时间:2024-10-26 23:40:13
本文介绍了自动映射器映射,从通用列表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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小提琴链接以显示此内容

更多推荐

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

本文发布于:2023-11-04 12:08:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!