AutoMapper12.0.1 扩展方法封装

主要是对于高版本的封装:

 public static class AutoMapperExt
    {
        /// <summary>
        ///  类型映射
        /// </summary>
        public static T MapTo<T>(this object obj)
        {
            if (obj == null) return default(T);

            var config = new MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(T)));
            var mapper = config.CreateMapper();
            return mapper.Map<T>(obj);
        }

        /// <summary>
        /// 集合列表类型映射
        /// </summary>
        public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
        {
            Type sourceType = source.GetType().GetGenericArguments()[0];  //获取枚举的成员类型
            var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
            var mapper = config.CreateMapper();

            return mapper.Map<List<TDestination>>(source);
        }

        /// <summary>
        /// 集合列表类型映射
        /// </summary>
        public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
        {
            var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
            var mapper = config.CreateMapper();

            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;

            var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
            var mapper = config.CreateMapper();
            return mapper.Map<TDestination>(source);
        }

    }
}

用于简化对象之间的类型映射操作。以下是对每个方法的简要解释:

  1. MapTo<T>(this object obj) 方法用于将一个对象映射为指定类型 T 的对象。
  2. MapToList<TDestination>(this IEnumerable source) 方法用于将一个集合列表映射为指定类型 TDestination 的对象列表。
  3. MapToList<TSource, TDestination>(this IEnumerable<TSource> source) 方法也是用于将一个集合列表映射为指定类型 TDestination 的对象列表,但是这里需要传入源类型和目标类型。
  4. MapTo<TSource, TDestination>(this TSource source, TDestination destination) 方法用于将一个源对象映射为目标对象,并返回目标对象。此方法还包含了对源和目标对象为空的判断。

相关推荐

  1. AutoMapper12.0.1 扩展方法封装

    2024-03-16 11:24:03       20 阅读
  2. c#扩展方法

    2024-03-16 11:24:03       33 阅读
  3. C#中的扩展方法

    2024-03-16 11:24:03       16 阅读
  4. 【.NET Core】匿名方法扩展方法

    2024-03-16 11:24:03       42 阅读
  5. C# 匿名方法扩展方法详解

    2024-03-16 11:24:03       14 阅读
  6. Redis Helper封装:静态方法

    2024-03-16 11:24:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-16 11:24:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-16 11:24:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-16 11:24:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-16 11:24:03       18 阅读

热门阅读

  1. RabbitMq多数据源配置

    2024-03-16 11:24:03       18 阅读
  2. 开发K8S Operator

    2024-03-16 11:24:03       18 阅读
  3. LeetCode 174.地下城游戏 Python题解

    2024-03-16 11:24:03       20 阅读
  4. 探索机器学习:智能时代的魔法

    2024-03-16 11:24:03       22 阅读
  5. Github 2024-03-13 C开源项目日报 Top10

    2024-03-16 11:24:03       17 阅读
  6. Python 算法交易实验68 回测对象重构

    2024-03-16 11:24:03       23 阅读
  7. HTML前置基础

    2024-03-16 11:24:03       16 阅读
  8. 00342第一章 概述 思考题和练习题(C语言)

    2024-03-16 11:24:03       17 阅读