using AutoMapper; namespace DS.Module.AutoMapper; /// /// AutoMapper扩展类 /// public static class AutoMapperExtension1 { /// /// 将源对象映射到目标对象 /// /// 实体类型 /// 源对象 /// 转化之后的实体 public static T MapTo(this object obj) { if (obj == null) return default; var config = new MapperConfiguration(ctx => ctx.CreateMap(obj.GetType(), typeof(T))); var mapper = config.CreateMapper(); return mapper.Map(obj); } /// /// 映射 /// /// /// /// /// public static T Map(object dto, T entity) { var config = new MapperConfiguration(ctx => ctx.CreateMap(dto.GetType(), typeof(T))); var mapper = config.CreateMapper(); var newModel = mapper.Map(dto, entity); return newModel; } /// /// 单个实体映射 /// /// 源对象类型 /// 目标对象类型 /// /// public static TDestination MapTo(this TSource source) { if (source == null) return default; var config = new MapperConfiguration(ctx => ctx.CreateMap()); var mapper = config.CreateMapper(); return mapper.Map(source); } /// /// 集合映射 /// /// 源对象类型 /// 目标对象类型 /// /// public static IEnumerable MapTo(this IList source) { var config = new MapperConfiguration(cfg => cfg.CreateMap()); var mapper = config.CreateMapper(); return mapper.Map>(source); } }