AutoMapper 扩展
public static class AutoMapperExtensions { public static T MapTo<T>(this object obj) { if (obj == null) return default(T); Mapper.CreateMap(obj.GetType(), typeof(T)); return Mapper.Map<T>(obj); } public static TDestination MapTo<T, TDestination>(this T source, TDestination destination) { if (source == null) return destination; Mapper.CreateMap(typeof(T), typeof(TDestination)); return Mapper.Map<T, TDestination>(source, destination); } public static IEnumerable<TDestination> MapToList<TDestination>(this IEnumerable source) { foreach (var first in source) { var type = first.GetType(); Mapper.CreateMap(type, typeof(TDestination)); break; } return Mapper.Map<List<TDestination>>(source); } public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source) { Mapper.CreateMap<TSource, TDestination>(); return Mapper.Map<List<TDestination>>(source); } }
Categories:
.net
已禁用评论。