Mapper类

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace CommonHelper
{
public class Mapper
{
public static object ToEntity(DataRow adaptedRow, Type entityType)
{
if (entityType == null || adaptedRow == null)
return null;
object entity = Activator.CreateInstance(entityType);
CopyToEntity(entity, adaptedRow);
return entity;
} public static T ToEntity<T>(DataRow adaptedRow) where T : new()
{
T item = new T();
if (adaptedRow == null)
return item;
item = Activator.CreateInstance<T>();
CopyToEntity(item, adaptedRow);
return item;
} public static void CopyToEntity(object entity, DataRow adaptedRow)
{
if (entity == null || adaptedRow == null)
return;
PropertyInfo[] propertyInfos = entity.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (!CanSetPropertyValue(propertyInfo, adaptedRow))
continue; try
{
if (adaptedRow[propertyInfo.Name] is DBNull)
{
propertyInfo.SetValue(entity, null, null);
continue;
}
SetPropertyValue(entity, adaptedRow, propertyInfo);
}
finally
{
}
}
} public static bool CanSetPropertyValue(PropertyInfo propertyInfo, DataRow adaptedRow)
{
if (!propertyInfo.CanWrite)
return false; if (!adaptedRow.Table.Columns.Contains(propertyInfo.Name))
return false; return true;
} public static void SetPropertyValue(object entity, DataRow adaptedRow, PropertyInfo propertyInfo)
{
if (propertyInfo.PropertyType == typeof(DateTime?) || propertyInfo.PropertyType == typeof(DateTime))
{
DateTime date = DateTime.MaxValue;
DateTime.TryParse(adaptedRow[propertyInfo.Name].ToString(),
CultureInfo.CurrentCulture, DateTimeStyles.None, out date); propertyInfo.SetValue(entity, date, null);
}
else
propertyInfo.SetValue(entity, adaptedRow[propertyInfo.Name], null);
}
}
}

mapper.cs

DataTable拓展方法

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CommonHelper.Extensions
{
/// <summary>
/// DataTable拓展方法
/// </summary>
public static class DateTableExtensions
{
/// <summary>
/// 将dataTable转换为实体类集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static List<T> ToCollection<T>(this DataTable table) where T : new()
{
if (table != null && table.Rows.Count > )
{
List<T> list = new List<T>();
foreach (DataRow dr in table.Rows)
{
list.Add(Mapper.ToEntity<T>(dr));
}
return list;
}
else
return new List<T>();
} /// <summary>
/// 将datattable第一行转换为实体类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static T FirstRowEntiy<T>(this DataTable table) where T :new()
{
if (table != null && table.Rows.Count > )
return Mapper.ToEntity<T>(table.Rows[]);
else
return default(T);
} /// <summary>
/// datatable转2标准Jon
/// </summary>
/// <param name="dt">DataTable数据源</param>
/// <param name="total">可选:转普通json传>0的值或忽略此参数;如果针对easyUI的json的一定要把total参数分页总行数传进来</param>
/// <returns></returns>
public static string ToJsonStr(this DataTable dt, int total)
{
System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
foreach (DataRow dr in dt.Rows)
{
try
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();//实例化一个参数集合
foreach (DataColumn column in dt.Columns)
{
dictionary.Add(column.ColumnName,dr[column.ColumnName].ToString();
}
arrayList.Add(dictionary);//arrarylist中添加键值
}
catch (Exception)
{ throw;
}
}
if (total>)//easyUI专用,分页的总行数
{
var dirctionary=new Dictionary<string,object>();
dirctionary.Add("total",total);//此参数是easyUI使用
dirctionary.Add("rows",arrayList);//此参数是easyUI使用
return JsonConvert.SerializeObject(dirctionary);//序列化参数
}
else
{
return JsonConvert.SerializeObject(arrayList);
}
}
}
}

反射 DataTable拓展方法 转实体对象、实体集合、JSON的更多相关文章

  1. C#实体对象序列化成Json并让字段的首字母小写的两种解决方法

    引言:最近在工作中遇到与某些API对接的post的数据需要将对象的字段首字母小写.解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性 ...

  2. 将form表单元素转为实体对象 或集合 -ASP.NET C#

    简介: 做WEBFROM开发的同学都知道后台接收参数非常麻烦 虽然MVC中可以将表单直接转为集实,但不支持表单转为 LIST<T>这种集合 单个对象的用法: 表单: <input n ...

  3. C#实体对象序列化成Json,并让字段的首字母小写

    引言:最近在工作中遇到与某些API对接的post的数据需要将对象的字段首字母小写.解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性 ...

  4. C#实体对象序列化成Json,格式化,并让字段的首字母小写

    解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性) public class UserInfo { [JsonProperty(& ...

  5. C#调用webservice 时如何传递实体对象

    在webservice端公开一个实体类,然后实例化,赋值,然后再给到webservice,可以实现,但是,即使调用端和service端的实体类完全一致,你也要重新实例化service端的,重新赋值,将 ...

  6. EBS OAF开发中实体对象和视图对象的属性设置器

    EBS OAF开发中实体对象和视图对象的属性设置器 (版权声明.本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 源文: Home > Oracle ...

  7. 利用java反射将结果集封装成为对象和对象集合

    java反射机制是什么 反射机制是在运行状态中,可以知道任何一个类的属性和方法,并且调用类的属性和方法: 反射机制能够做什么 1.判断运行对象的所属类 2.构造任意一个类的对象 3.获取任意一个类的属 ...

  8. 使用jackson工具类把对象或集合转为JSON格式

    jackson使用方法: 1.加入jar包: jackson-annotations-2.2.2.jar jackson-core-2.2.2.jar jackson-databind-2.2.2.j ...

  9. DataTable转换为Model实体对象

    记得在学校的时候,接触得最多的就是SqlHelper,每次在读取的时候不管是DataTable还是DataReader转换为实体对象的时候是最恼火的,因为要写很多代码,而且没有什么意义.后面接触到了反 ...

随机推荐

  1. DateTime.TryParseExact 万能时间格式转化

    本文转载:http://blog.csdn.net/gaofang2009/article/details/6073231 前天同事问C#有没有相关的方法能把"年月日时分秒"这样的 ...

  2. MATLAB中导入数据:importdata函数

    用load函数导入mat文件大家都会.可是今天我拿到一个数据,文件后缀名竟然是'.data'.该怎么读呢? 我仅仅好用matlab界面Workspace区域的"import data&quo ...

  3. 利用Ajax把前端的数据封装成JSON格式发送到服务器端并写成XML格式在服务器的硬盘上

    1.首先要在前端把要发送的东西(这里是一个实例化的car对象)都准备好,利用Ajax发送到服务器端,代码如下: <html xmlns="http://www.w3.org/1999/ ...

  4. WinCacheGrind配合XDebug分析PHP程序性能

    http://www.nowamagic.net/librarys/veda/detail/2338

  5. bash if 表达式

    .bash把[[ $a -lt $b ]]看作一个单独的元素,并且返回一个退出码.退出码0为真,非零为假 例如: a= b=c [[ $a -lt $b ]] echo $? # a小于b为真 [[ ...

  6. 【转】cocos2d-x中锚点设置及定位方式

    http://blog.csdn.net/wayne5ning/article/details/8160506 说在前面:以下是基于cocos2d-2.0-x-2.0.3作的总结 问题 在cocos2 ...

  7. 用标准版的Eclipse搭建PHP环境

    用标准版的Eclipse搭建PHP环境 ——@梁WP 摘要:用标准版的Eclipse搭建PHP环境. 一.下载Eclipse的PHP插件 百度搜索phpeclipse,看到某条结果是带有SourceF ...

  8. springMvc解决json中文乱码

    springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...

  9. 第一篇:Mysql操作初级

    Mysql操作初级   Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如: ...

  10. /etc目录深入理解

    /etc This is the nerve center of your system, it contains all system related configuration files in ...