AutoMapper可以很方便的将一个实体的属性值转化给另一个对象。这个功能在我们日常的编码中经常会遇到。我将AutoMapper的一些基本映射功能做成扩展方法,在编码中更方便使用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using System.Collections;
using System.Data;
using System.Reflection; namespace NFMES.Core.Type
{
public static class AutoMapperExtension
{
/// <summary>
/// 实体对象转换
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public static TDestination MapTo<TDestination>(this object o)
{
if (o == null)
throw new ArgumentNullException(); Mapper.CreateMap(o.GetType(), typeof(TDestination)); return Mapper.Map<TDestination>(o); ;
} /// <summary>
/// 集合转换
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public static List<TDestination> MapTo<TDestination>(this IEnumerable o)
{
if (o == null)
throw new ArgumentNullException(); foreach (var item in o)
{
Mapper.CreateMap(item.GetType(), typeof(TDestination)); break;
}
return Mapper.Map<List<TDestination>>(o);
} /// <summary>  
/// 将 DataTable 转为实体对象  
/// </summary>  
/// <typeparam name="T"></typeparam>  
/// <param name="dt"></param>  
/// <returns></returns>  
public static List<T> MapTo<T>(this DataTable dt)
{
if (dt == null || dt.Rows.Count == )
return default(List<T>); Mapper.CreateMap<IDataReader, T>();
return Mapper.Map<List<T>>(dt.CreateDataReader());
} /// <summary>
/// 将List转换为Datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable MapToTable<T>(this IEnumerable list)
{
if (list == null)
return default(DataTable); //创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//获得反射的入口
System.Type type = typeof(T);
DataTable dt = new DataTable();
//把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in list)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
} }
}

这个静态类中有4个扩展方法,分别对Object类型,IEnumberable类型,DataTable类型添加了MapTo方法,可以方便的将对象映射到对象,集合映射到集合,表映射到集合,集合映射到表(这个功能AutoMapper不支持,我用反射实现的)

单实体转化使用方式:

集合转化的使用方式:

AutoMapper还有很多功能,我这个类只扩展了一些最基本和常用的功能,以后用到会继续添加。

C# AutoMapper的简单扩展的更多相关文章

  1. 对bootstrap modal的简单扩展封装

    对bootstrap modal的简单扩展封装 参考自:http://www.muzilei.com/archives/677   注:原文不支持bootstrap新版本,并且居中等存在问题 此段时间 ...

  2. Log4net创建日志及简单扩展

    转:http://blog.csdn.net/CHENFEIYANG2009/article/details/5397342 1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log ...

  3. AutoMapper 6.x 扩展

    简介 很多时候我们使用AutoMapper的时候,都需要进行一个配置才可以使用Mapper.Map<Source,Target>(entity);.如果不进行配置则会报错. 如果实体过多, ...

  4. 使用Log4net创建日志及简单扩展

    如何使用Log4net创建日志及简单扩展 1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的 ...

  5. OOM AutoMapper的简单实用

    OOM  AutoMapper的简单实用 一.前言: OOM顾名思义,Object-Object-Mapping实体间相互转换,AutoMapper也是个老生常谈了,其意义在于帮助你无需手动的转换简单 ...

  6. AutoMapper的简单使用

    接触AutoMapper已经有两年多的时间了,在ORM框架中,它使持久层对象与DTO对象之间的转换变得相当简单. 随着负责的项目的增多,使用的技术框架一多起来,很多具体的技术点难免记不清, 加上同时兼 ...

  7. automapper的简单用法

    AutoMapper对象转换方面(Object-Object Mapping)对象映射工具,实现对象和对象之间的转化.主要应用在项目的dto,model,entity或viewmodel之间转换,其实 ...

  8. AutoMapper 6.x 扩展方法

    简介 很多时候我们使用AutoMapper的时候,都需要进行一个配置才可以使用Mapper.Map<Source,Target>(entity);.如果不进行配置则会报错. 如果实体过多, ...

  9. 由一道面试题简单扩展 — setTimeout、闭包

    在一个前端公众号,看到这么一个号称简单的面试题: 1.以下程序输出什么? <script type="text/javascript"> function init() ...

随机推荐

  1. 符合RESTful规范的API

    统一使用的utils,serializers: class BaseResponse: def __init__(self): self.code = 1000 self.data = None se ...

  2. 分布式锁实践(一)-Redis编程实现总结

    写在最前面 我在之前总结幂等性的时候,写过一种分布式锁的实现,可惜当时没有真正应用过,着实的心虚啊.正好这段时间对这部分实践了一下,也算是对之前填坑了. 分布式锁按照网上的结论,大致分为三种:1.数据 ...

  3. TMS Grid

    TMS Grid http://edn.embarcadero.com/article/42553

  4. c# 数据集调试工具插件

    DataSetSpySetup,调试期查看dataset数据集的记录内容, Debug DataSet

  5. Display file information in the document window

    [Display file information in the document window] The status bar is located at the bottom of every d ...

  6. springmvc web.xml配置之 -- DispatcherServlet

    springMVC servlet配置与启动 看一下springmvc的web.xml常见配置: <servlet> <!-- 配置DispatcherServlet --> ...

  7. 第七章 二叉搜索树 (a)概述

  8. js中__proto__, property, prototype, 对象自身属性方法和原型中的属性方法的区别

    __proto__: 这个属性是实例对象的属性,每个实例对象都有一个__proto__属性,这个属性指向实例化该实例的构造函数的原型对象(prototype). proterty:这个方法是对象的属性 ...

  9. Linux基石【第一篇】VMware上安装Centos及配置

    一.安装VMware软件 首先,下载个VMware软件,直接百度:VMware,然后找到可以下载的就可以 然后按步骤安装即可,安装完后,双击打开 二.安装Centos系统 打开VMware虚拟机,然后 ...

  10. 岛屿的个数12 · Number of Islands12

    [抄题]: 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. [ [1, 1, 0, 0, 0], [0, 1, 0, 0 ...