AutoMapper帮助类

     /// <summary>
     /// AutoMapper帮助类
     /// </summary>
     public static class AutoMapperHelper
     {
         /// <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<TDestination> source)
         {
             Type sourceType = source.GetType().GetGenericArguments()[];  //获取枚举的成员类型
             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);
         }
     }

AutoMapFromAttribute

 public class AutoMapFromAttribute : AutoMapAttributeBase
     {
         public MemberList MemberList { get; set; }

         public AutoMapFromAttribute(params Type[] targetTypes)
             : base(targetTypes)
         {
         }

         public AutoMapFromAttribute(MemberList memberList, params Type[] targetTypes)
             : this(targetTypes)
         {
             this.MemberList = memberList;
         }

         public override void CreateMap(IMapperConfigurationExpression configuration, Type type)
         {
             )
                 return;
             foreach (Type targetType in this.TargetTypes)
                 configuration.CreateMap(targetType, type, MemberList.Destination);
         }
         public static bool IsNullOrEmpty<T>(ICollection<T> source)
         {
             if (source != null)
                 ;
             return true;
         }

     }

AutoMapAttributeBase

  public abstract class AutoMapAttributeBase : Attribute
     {
         public Type[] TargetTypes { get; private set; }

         protected AutoMapAttributeBase(params Type[] targetTypes)
         {
             this.TargetTypes = targetTypes;
         }
         public abstract void CreateMap(IMapperConfigurationExpression configuration, Type type);
     }

AutoMapper 帮助类的更多相关文章

  1. 使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到实体类

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

  2. AutoMapper随笔记

    平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html#skill 先看效果:(完整Demo:https://git ...

  3. AutoMapper简单用法

    首先在NuGet添加AutoMapper /// <summary> /// AutoMapper帮助类 /// </summary> public static class ...

  4. C#进阶系列——DDD领域驱动设计初探(五):AutoMapper使用

    前言:前篇搭建了下WCF的代码,就提到了DTO的概念,对于为什么要有这么一个DTO的对象,上章可能对于这点不太详尽,在此不厌其烦再来提提它的作用: 从安全上面考虑,领域Model都带有领域业务,让Cl ...

  5. 升级AutoMapper后遇到的“Missing map”与“Missing type map configuration”问题

    前几天发现 AutoMapper 3.3 的一个性能问题(详见:遭遇AutoMapper性能问题:映射200条数据比100条慢了近千倍),于是将 AutoMapper 升级至最新的 5.1.1 看是否 ...

  6. AutoMapper中的Map和DynamicMap——高手注重细节,思考和总结

    近日在做项目的时候,遇到了个怪问题,关于AutoMapper的细节问题,也是不为一般人所关注的. 本人研究AutoMapper也没有多长时间,而且研究的过程中也写了关于AutoMapper的系列基础教 ...

  7. AutoMapper 使用总结

    初识AutoMapper 在开始本篇文章之前,先来思考一个问题:一个项目分多层架构,如显示层.业务逻辑层.服务层.数据访问层.层与层访问需要数据载体,也就是类.如果多层通用一个类,一则会暴露出每层的字 ...

  8. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)

    前言 Hi,大家好,我是Rector 时间飞逝,一个星期又过去了,今天还是星期五,Rector在图享网继续跟大家分享系列文本:一步一步创建ASP.NET MVC5程序[Repository+Autof ...

  9. Asp.Net AutoMapper用法

    1.AutoMapper简介 用于两个对象映射,例如把Model的属性值赋值给View Model.传统写法会一个一个属性的映射很麻烦,使用AutoMapper两句代码搞定. 2.AutoMapper ...

随机推荐

  1. react父子组件各自生命周期函数加载的先后顺序

    理解记忆总结: 父组件即将挂载(最外层的父组件都还没准备进入,其内部的子组件当然更没进入了) -> 子组件即将挂载  -> 子组件挂载完成(父内部都没完成,父当然不能算完成)  -> ...

  2. jquery操作select(选中,取值)

    最近工作中总出现select 和 option问题,整理一下,内容大部分源于网络资料 一.基础取值问题 例如<select class="selector"></ ...

  3. 关于webpack 配置文件找不到

    运行命令  npm run eject 将配置文件解压出来 如果运行这个命令有错的时候,很可能与 git 有关 这时候,打开项目文件夹,显示所有隐藏的文件夹(工具),如果显示了git 的文件夹  删掉 ...

  4. Java基础之引用(String,char[],Integer)总结于牛客网的专项练习题

    1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...

  5. c# copy类中值到另外一个对象中

    贴图: 调用:

  6. Windows Server 2008 R2 /2012 修改密码策略(摘抄 原文地址 https://www.cnblogs.com/mili3/p/7799347.html)

    今天建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点. 问题:    在“管理工具 >> 本地安全策略 > ...

  7. [翻译] SvpplyTable

    SvpplyTable https://github.com/liuminqian/SvpplyTable SvpplyTable is a demo to realize expandable an ...

  8. 尝试Office 2003 VSTO的开发、部署

    转载:http://www.cnblogs.com/oneivan/p/4243574.html 背景:一年前,某项目需要使用到Excel进行数据录入,考虑到很多用户还是使用XP+Office 200 ...

  9. 附加进程找不到w3wp.exe进程解决方案

    在进程列表的下面,有个show processes in all sessions(显示所有用户的进程(U)),把它勾上就能看到了 ,就是这么简单.

  10. canvas学习笔记1

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...