自动映射自定义泛型类型 - 如何? [英] AutoMapping custom Generic Types - How?
问题描述
我使用的是 automapper 1.1.0.188版本
hey guys, I'm using automapper version 1.1.0.188
在我AutoMapper.Configure我映射到实体DTO的,反之亦然,就像这样:
In my AutoMapper.Configure I'm mapping Entities to DTOs and vice versa, like so:
// entity >> DTO
Mapper.CreateMap<MetaTemplate, MetaTemplateDTO>();
Mapper.CreateMap<Person, PersonDTO>();
// DTO >> Entity
Mapper.CreateMap<MetaTemplateDTO, MetaTemplate>();
Mapper.CreateMap<PersonDTO, Person>();
当我做下面的映射(反之亦然),一切工作正常。
When I do the below mappings (and vice versa) everything works fine
Mapper.Map<entity, entityDTO>(entity);
Mapper.Map<List<entity>, List<entityDTO>>(entities);
请注意上面automapper仅仅适用于表&LT;>没有我不必进行任何配置
Note above that automapper just works with List<> without me having to configure anything.
我有一个一般容器(简化这个例子):
I have a Generic Container (simplified for this example):
public class Container<T>
{
public int TotalItems{get;set;}
public IList<T> Items{get;set;}
}
现在,没有任何多余的自动映射配置,当我这样做:
Now, without any extra automapping config, when I do:
Mapper.Map<Container<entity>, Container<entityDTO>>(entityContainer);
我得到一个automapper异常:
I get an automapper exception:
缺少类型映射配置或不支持mapping.Exception
Missing type map configuration or unsupported mapping.Exception
不过,如果我在自动映射配置加入这一行特定类型,如下所示,则容器测绘工作。
However, if I add this line in the automap configure for a specific type, as below, then the Container mapping works.
Mapper.CreateMap<Container<PersonDTO>, Container<Person>>();
然而,它只会为个人/ PersonDTO类型的工作。
However, it will ONLY work for that Person/PersonDTO type.
这是为什么?我怎样才能让automapper识别容器类,因为它承认名单,其中,> ??
Why is this? How can I make automapper recognize the Container class as it recognizes List<>??
我不想明确配置映射每一种类型了。
I don't want to explicitly configure mappings for every type AGAIN.
凉,欢呼声
推荐答案
答案是Automapper只是不处理不支持的类型,即使你使用的类型是支持的类型的容器。
The answer is that Automapper just doesn't deal with unsupported types, even if the Type you're using is a container of supported types.
解决的办法是手动映射对象
The solution was to map the object manually
这篇关于自动映射自定义泛型类型 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!