1#

首先我们要拥有dll:

官方下载:https://github.com/AutoMapper/AutoMapper/downloads

AutoMapper.3.0.0.rar (, 下载次数:282)


1.建立一个工具类:

  1. public class MapperHelp
  2.     {
  3.         public static TDest Convert<TSrc, TDest>(TSrc src)
  4.         {
  5.             return AutoMapper.Mapper.Map<TSrc, TDest>(src);
  6.         }

  7.         public static void MakeMap<TSrc, TDest>()
  8.         {
  9.             AutoMapper.Mapper.CreateMap<TSrc, TDest>();
  10.             AutoMapper.Mapper.CreateMap<TDest, TSrc>();
  11.         }

  12.         public static void MakeSingleMap<TSrc, TDest>()
  13.         {
  14.             AutoMapper.Mapper.CreateMap<TSrc, TDest>();
  15.         }
  16.     }
复制代码

2.建立一个实例化的方法

  1. public class ModuleInit
  2.     {
  3.         public static void Init()
  4.         {
  5.             InitMappers();
  6.         }

  7.         private static void InitMappers()
  8.         {
  9.             //todo: add mappers
  10.             MapperHelp.MakeSingleMap<CustomerDetail, CustomerDetail>();
  11.         }
  12.     }
复制代码

3.在global.asax Application_Start事件中初始化调用

  1. ModuleInit.Init();
复制代码

4.这样便能调用了,targetObj便是复制好的目标对象

  1. var targetObj= MapperHelp.Convert<CustomerDetail, CustomerDetail>(detail);
复制代码