1.创建一个控制台
2.打开Nuget,安装 AutoMapper包
控制台代码:
using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Test.Models; namespace Test { class Program { static void Main(string[] args) { Patient pt = new Patient { Name = "ddd", Age = 14, Sex = "男" }; Mapper.Initialize(x=>x.CreateMap<Patient,TT>()); TT t = Mapper.Map<Patient, TT>(pt); Console.WriteLine(t.Body); Console.WriteLine(t.Num); Console.WriteLine(t.Sum); Console.Read(); } } }
2.创建Model Patient.cs、TT.cs
using AutoMapper.Configuration.Conventions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test.Models { public class Patient { [MapTo("Body")] public string Name { get; set; } [MapTo("Num")] public int Age { get; set; } [MapTo("Sum")] public string Sex { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test.Models { public class TT { public string Body { get; set; } public string Num { get; set; } public string Sum { get; set; } } }
断点执行,注意 TT.cs 中 Num 的类型 和 Patient.cs 中 Age 的类型不一样,结果都能够转换。
感谢:https://zhidao.baidu.com/question/1607626619722621507.html
http://blog.csdn.net/csethcrm/article/details/52934325
补充:
如果转换的是List列表时候,需要初始化映射下,不然会报错(但是有个问题撒,由于Mapper.Initialize 只有第一次初始化使用才有效,再次使用会报错)
var waitVisitInfo = _doctorBLL.GetCheckWaitQueue(param); if (waitVisitInfo.Count == 0) { result.SetError("", "没有找到数据!"); } else { //初始化映射 和单个对象的映射一样 Mapper.Initialize(cfg => cfg.CreateMap<GetCheckWaitQueueById_Result, GetCheckWaitQueueOut>()); List<GetCheckWaitQueueOut> waitVisitList = Mapper.Map<List<GetCheckWaitQueueById_Result>, List<GetCheckWaitQueueOut>>(waitVisitInfo); //开始转换 var data = (from c in waitVisitList select new { item = c }).ToList(); if (waitVisitList.Count == 0) { result.SetError("", "没有找到数据!"); } else { result.SetSuccess(data); } }
所以,最后使用在 AutoMapperConfig.cs 中,配置 Mapper.Initialize的对象
public class AutoMapperConfig { /// <summary> /// /// </summary> public static void Config() { Mapper.Initialize(m => { m.CreateMap<GetDeptById_Result, GetDeptListOut>(); m.CreateMap<GetDoctorListById_Result, GetDoctorListOut>(); m.CreateMap<GetCurrentRegById_Result, GetCurrentRegOut>(); m.CreateMap<GetAppointRegStatisticsById_Result, GetAppointRegStatisticsOut>(); m.CreateMap<GetAppointRegById_Result, GetAppointRegOut>(); m.CreateMap<GetPendingRegQueryById_Result, GetPendingRegQueryOut>(); m.CreateMap<GetWaitVisitQueueById_Result, GetWaitVisitQueueOut>(); m.CreateMap<GetCheckWaitQueueById_Result, GetCheckWaitQueueOut>(); }); } }
最后,需要在 Global.asax 中 Application_Start 配置 AutoMapperConfig.Config();
protected void Application_Start() { // AutoMapperConfig.Config(); GlobalConfiguration.Configure(WebApiConfig.Register); AutoMapperConfig.Config(); }
感谢:https://www.cnblogs.com/wuyunblog/p/6666485.html
https://stackoverflow.com/questions/38999771/test-method-x-threw-exception-system-invalidoperationexception-mapper-not-in