C#拓展方法,自动映射类
2020-12-28 11:04 胡dot 阅读(377) 评论(0) 编辑 收藏 举报public static class Mapper { /// <summary> /// 类型映射,默认字段名字一一对应 /// </summary> /// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam> /// <typeparam name="TSource">要被转化的实体,Entity</typeparam> /// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param> /// <returns>转化之后的实体</returns> public static TDestination MapTo<TSource,TDestination>(this TSource source) where TDestination : class { if (source == null) return default(TDestination); var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>()); var mapper = config.CreateMapper(); return mapper.Map<TDestination>(source); } /// <summary> /// 集合列表类型映射,默认字段名字一一对应 /// </summary> /// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam> /// <typeparam name="TSource">要被转化的实体,Entity</typeparam> /// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param> /// <returns>转化之后的实体列表</returns> public static IEnumerable<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source) where TDestination : class where TSource : class { if (source == null) return new List<TDestination>(); var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>()); var mapper = config.CreateMapper(); return mapper.Map<List<TDestination>>(source); } }
在对象后小数点就可以调用映射方法