Automapper继承映射工厂

时间:2015-10-26 21:07:34

标签: automapper

我有以下课程:

public class A{}
public class AA : A {public int aa{get;set;}}
public class AAA : A {public int aaa{get;set;}}
public class B{}
public class BB : B {public int bb{get;set;}}
public class BBB : B {public int bbb{get;set;}}

这是我的映射:

Mapper.CreateMap<A,B>().include<AA,BB>().include<AAA,BBB>();
Mapper.CreateMap<AA,BB>();
Mapper.CreateMap<AAA,BBB>();

我假设automapper将理解hiearchy并映射相应的派生类。但我无法通过以下代码获得它。映射的实体始终是基类,在本例中为B. 请注意我有一个存储库工厂,它将确定GetAll()的返回类型。

var list = myRepo.GetAll() //this returns IEnumerabe<AA> or IEnumerable<AAA> 
var newlist = Mapper.Map<IEnumerable<B>>(list); //I wanted to use "B" here due to the fact I will have many inherited classes of B. I don't want to restrict to a specific child class of B.

1 个答案:

答案 0 :(得分:0)

行。上面的代码应该有效。不幸的是,我使用的是自动播放器4.1.1并且存在一个错误 - https://github.com/AutoMapper/AutoMapper/issues/819