using AutoMapper; using System.Collections.Generic; namespace CompressorXN_HelperLib { public static class CustomAutoMapHelper { /// /// 类型映射,默认字段名字一一对应 /// /// 转化之后的model,可以理解为viewmodel /// 要被转化的实体,Entity /// 可以使用这个扩展方法的类型,任何引用类型 /// 转化之后的实体 public static TDestination MapTo(this TSource source) where TDestination : class where TSource : class { if (source == null) return default; var config = new MapperConfiguration(cfg => cfg.CreateMap()); var mapper = config.CreateMapper(); return mapper.Map(source); } /// /// 集合列表类型映射,默认字段名字一一对应 /// /// 转化之后的model,可以理解为viewmodel /// 要被转化的实体,Entity /// 可以使用这个扩展方法的类型,任何引用类型 /// 转化之后的实体列表 public static IEnumerable MapToList(this IEnumerable source) where TDestination : class where TSource : class { if (source == null) return new List(); var config = new MapperConfiguration(cfg => cfg.CreateMap()); var mapper = config.CreateMapper(); return mapper.Map>(source); } } }