6

I've just started using AutoMapper and so far found it very straight-forward and time-saving.

Just one thing I'm not sure about - how do I map all the properties of a given type in the same way?

Can this be done with AutoMapper in a single statement, using a lambda, as with regular mapping?

4

1 回答 1

6

您要查找的内容称为CustomTypeConverter. 这些是全局范围的,只需要配置一次。

语法是:

Mapper.CreateMap<TSourceProperty,TDestinationProperty>().ConvertUsing(argument);

argument可以在哪里

  1. 一个实现ITypeConverter<TSourceProperty,TDestinationProperty>
  2. 一个Func<TSourceProperty,TDestinationProperty>

Jimmy Bogard 在http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/05/automapper-feature-custom-type-converters.aspx上有一篇关于实现CustomTypeConverters 的文章。

AutoMapper 文档的 CustomTypeConverter 页面还提供了更多信息。

哦,顺便说一下(因为我想要 Omu 的赏金)你也可以通过切换到 valueinjecter 来做到这一点。

于 2011-03-20T17:23:31.970 回答