using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TProduct { /// /// AutoMapper帮助类 /// public static class AutoMapperHelper { /// /// 获取单个 /// /// /// /// /// public static Out GetSingle(In obj) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap()).CreateMapper(); return mapper.Map(obj); } public static Out MapTo(this In obj) { return GetSingle(obj); } /// /// 映射单个实例 /// /// /// /// /// public static void MapSingle(In inner, Out outer) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap()).CreateMapper(); mapper.Map(inner, outer); } /// /// 映射多个实例 /// /// /// /// /// public static void MapMultiple(IEnumerable innerList, IEnumerable outerList) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap()).CreateMapper(); mapper.Map(innerList, outerList); } public static void Map(this In inner, Out outer) { MapSingle(inner, outer); } public static void Map(this IEnumerable innerList, IEnumerable outerList) { MapMultiple(innerList, outerList); } /// /// 获取列表 /// /// /// /// /// public static List GetMultiple(IEnumerable objs) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap()).CreateMapper(); return mapper.Map, List>(objs); } public static List MapTo(this IEnumerable objs) { return GetMultiple(objs); } } }