AutoMapper5.0创建对象方法更新

来源:互联网 发布:plc编程指令大全 编辑:程序博客网 时间:2024/05/19 19:34
 /// <summary>        ///  单个对象映射        /// </summary>        public static TDestination MapTo<TSource, TDestination>(TSource source)        {            if (source == null) return default(TDestination);            Mapper.Initialize(x=>x.CreateMap(typeof(TSource), typeof(TDestination)));            return Mapper.Map<TDestination>(source);        }        /// <summary>        /// 集合列表类型映射        /// </summary>        public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)        {            if (source == null) return default(List<TDestination>);            Mapper.Initialize(x => x.CreateMap(typeof(TSource), typeof(TDestination)));            return Mapper.Map<List<TDestination>>(source);        }        /// <summary>        /// 类型映射        /// </summary>        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)            where TSource : class            where TDestination : class        {            if (source == null) return destination;            Mapper.Initialize(x => x.CreateMap(typeof(TSource), typeof(TDestination)));            return Mapper.Map(source, destination);        }

0 0
原创粉丝点击