如何注册AutoMapper定制ObjectMapper [英] How to register a custom ObjectMapper with AutoMapper

查看:690
本文介绍了如何注册AutoMapper定制ObjectMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算创建自己的自定义对象映射器使用AutoMapper的IObjectMapper接口的类型,但我没有看到在这里我们可以注册AutoMapper实现的映射器的任何地方。 。谁能告诉我如何注册

I am planning to create my Own Custom Object Mapper for a type using AutoMapper's IObjectMapper interface, but i don't see any place where we can register the implemented mapper with AutoMapper. Could anyone tell me how to register it.

编辑:有关它的更多信息,请遵循的讨论在AutoMapper-Users组

For more information on this please follow the discussion at AutoMapper-Users group

推荐答案

去这里的一个方法是,以取代静态的注册表函数在MapperRegistry类。下面是当前版本:

One way to go here is to replace the static registry function in the MapperRegistry class. Here's the current version:

public static Func<IEnumerable<IObjectMapper>> AllMappers = () => new IObjectMapper[]
{
#if !SILVERLIGHT
    new DataReaderMapper(),
#endif
    new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()),
    new StringMapper(),
    new FlagsEnumMapper(),
    new EnumMapper(),
    new ArrayMapper(),
    new EnumerableToDictionaryMapper(),
    new DictionaryMapper(),
#if !SILVERLIGHT
    new ListSourceMapper(),
#endif
    new EnumerableMapper(),
    new AssignableMapper(),
    new TypeConverterMapper(),
    new NullableMapper()
};

您会基本上复制了一个,这样做:

You'd basically copy that one, and do something like:

MapperRegistry.AllMappers = () => new IObjectMapper[] {
   // Insert your custom mapper somewhere in this list
};



就做,你做任何Mapper.CreateMap或Mapper.Initialize业务之前。映射器才能进行评估。

Just do that before you do any Mapper.CreateMap or Mapper.Initialize business. Mappers are evaluated in order.

这篇关于如何注册AutoMapper定制ObjectMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆