需要引用AutoMapper的Nuget包
 如果需要忽略某个字段就在字段上面打标签如下:

 [IgnoreMap]
 public string IgnoreValue { get; set; }

    /// <summary>
/// 对象映射
/// </summary>
public static class Extensions
{
/// <summary>
/// 同步锁
/// </summary>
private static readonly object Sync = new object(); /// <summary>
/// 将源对象映射到目标对象
/// </summary>
/// <typeparam name="TSource">源类型</typeparam>
/// <typeparam name="TDestination">目标类型</typeparam>
/// <param name="source">源对象</param>
/// <param name="destination">目标对象</param>
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
{
return MapTo<TDestination>(source, destination);
} /// <summary>
/// 将源对象映射到目标对象
/// </summary>
/// <typeparam name="TDestination">目标类型</typeparam>
/// <param name="source">源对象</param>
public static TDestination MapTo<TDestination>(this object source) where TDestination : new()
{
return MapTo(source, new TDestination());
} /// <summary>
/// 将源对象映射到目标对象
/// </summary>
private static TDestination MapTo<TDestination>(object source, TDestination destination)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (destination == null)
throw new ArgumentNullException(nameof(destination));
var sourceType = GetType(source);
var destinationType = GetType(destination);
var map = GetMap(sourceType, destinationType);
if (map != null)
return Mapper.Map(source, destination);
lock (Sync)
{
map = GetMap(sourceType, destinationType);
if (map != null)
return Mapper.Map(source, destination);
InitMaps(sourceType, destinationType);
}
return Mapper.Map(source, destination);
} /// <summary>
/// 获取类型
/// </summary>
private static Type GetType(object obj)
{
var type = obj.GetType();
if ((obj is System.Collections.IEnumerable) == false)
return type;
if (type.IsArray)
return type.GetElementType();
var genericArgumentsTypes = type.GetTypeInfo().GetGenericArguments();
if (genericArgumentsTypes == null || genericArgumentsTypes.Length == 0)
throw new ArgumentException("泛型类型参数不能为空");
return genericArgumentsTypes[0];
} /// <summary>
/// 获取映射配置
/// </summary>
private static TypeMap GetMap(Type sourceType, Type destinationType)
{
try
{
return Mapper.Configuration.FindTypeMapFor(sourceType, destinationType);
}
catch (InvalidOperationException)
{
lock (Sync)
{
try
{
return Mapper.Configuration.FindTypeMapFor(sourceType, destinationType);
}
catch (InvalidOperationException)
{
InitMaps(sourceType, destinationType);
}
return Mapper.Configuration.FindTypeMapFor(sourceType, destinationType);
}
}
} /// <summary>
/// 初始化映射配置
/// </summary>
private static void InitMaps(Type sourceType, Type destinationType)
{
try
{
var maps = Mapper.Configuration.GetAllTypeMaps();
Mapper.Initialize(config => {
ClearConfig();
foreach (var item in maps)
config.CreateMap(item.SourceType, item.DestinationType);
config.CreateMap(sourceType, destinationType);
});
}
catch (InvalidOperationException)
{
Mapper.Initialize(config => {
config.CreateMap(sourceType, destinationType);
});
}
} /// <summary>
/// 清空配置
/// </summary>
private static void ClearConfig()
{
var typeMapper = typeof(Mapper).GetTypeInfo();
var configuration = typeMapper.GetDeclaredField("_configuration");
configuration.SetValue(null, null, BindingFlags.Static, null, CultureInfo.CurrentCulture);
} /// <summary>
/// 将源集合映射到目标集合
/// </summary>
/// <typeparam name="TDestination">目标元素类型,范例:Sample,不要加List</typeparam>
/// <param name="source">源集合</param>
public static List<TDestination> MapToList<TDestination>(this System.Collections.IEnumerable source)
{
return MapTo<List<TDestination>>(source);
}
}

  

最新文章

  1. C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母
  2. http 报文
  3. 【qt4.8.6】qt-everywhere-opensource-src-4.8.6静态库编译,搭建vs2010 + Qt4.8.6环境
  4. windows 下 apache设置
  5. HUD-4602 Partition 排列
  6. window环境下 node.js 游戏框架pomelo 安装与启动
  7. 管理http服务的脚本
  8. 工作的准备:atoi,itoa,strcpy,memcpy,strcmp,二分查找,strcat
  9. Matlab图像处理系列1———线性变换和直方图均衡
  10. Android 2.3 版本中链接边框问题解决
  11. toggle的用法(点击更换不同的function)当指定元素被点击时,在两个或多个函数之间轮流切换。
  12. PERL学习笔记---正则表达式的应用
  13. iOS XIB使用中适配iPhoneX的安全区域、调用UiView动画
  14. C# Winform 登录中的忘记密码及自动登录
  15. HGOI20181031 模拟题解
  16. 06-python opencv 使用摄像头捕获视频并显示
  17. HDU 2157 How many ways?? (邻接矩阵快速幂)
  18. hive 相关异常
  19. linux如何手动释放linux内存
  20. 关于swift 单元测试的补充

热门文章

  1. WPF 简单的绕圈进度条(无cs代码)
  2. 零元学Expression Blend 4 - Chapter 15 用实例了解互动控制项「Button」I
  3. 处理 Windows Phone 应用中的“后退”按钮 (XAML)
  4. UWP中String 转为Path Data
  5. AndroidStudio问题汇总
  6. Android零基础入门第73节:Activity初入门,创建和配置如此简单
  7. TopFreeTheme精选免费模板【20130626】
  8. C#爬虫与反爬虫--字体加密篇
  9. Qemu搭建ARM vexpress开发环境(二)----通过u-boot启动Linux内核
  10. C语言实现常用排序算法——插入排序