AutoMapping自定义通用类型 – 如何?

嘿伙计们,我正在使用automapper版本1.1.0.188

在我的AutoMapper.Configure中,我将实体映射到DTO,反之亦然,如下所示:

// entity >> DTO Mapper.CreateMap(); Mapper.CreateMap(); // DTO >> Entity Mapper.CreateMap(); Mapper.CreateMap(); 

当我做下面的映射(反之亦然)时,一切正常

 Mapper.Map(entity); Mapper.Map<List, List>(entities); 

请注意,automapper只能与List 一起使用,而无需配置任何内容。

我有一个Generic Container(本例简化):

 public class Container { public int TotalItems{get;set;} public IList Items{get;set;} } 

现在,当我这样做时,没有任何额外的自动配置:

 Mapper.Map<Container, Container>(entityContainer); 

我得到一个automapperexception:

缺少类型映射配置或不支持的mapping.Exception

但是,如果我在特定类型的自动配置中添加此行,如下所示,则Container映射有效。

 Mapper.CreateMap<Container, Container>(); 

但是,它只适用于Person / PersonDTO类型。

为什么是这样? 如何让automapper识别Container类,因为它识别List ??

我不想为每种类型的AGAIN显式配置映射。

很酷,干杯

如果您的通用容器类的行为类似于对象列表,那么您最好实现IEnumerable接口。 然后,automapper应该能够迭代对象并相应地映射它们。

答案是,即使您使用的Type是受支持类型的容器,Automapper也不会处理不受支持的类型。

解决方案是手动映射对象