反射 DataTable拓展方法 转实体对象、实体集合、JSON
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的更多相关文章
- C#实体对象序列化成Json并让字段的首字母小写的两种解决方法
引言:最近在工作中遇到与某些API对接的post的数据需要将对象的字段首字母小写.解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性 ...
- 将form表单元素转为实体对象 或集合 -ASP.NET C#
简介: 做WEBFROM开发的同学都知道后台接收参数非常麻烦 虽然MVC中可以将表单直接转为集实,但不支持表单转为 LIST<T>这种集合 单个对象的用法: 表单: <input n ...
- C#实体对象序列化成Json,并让字段的首字母小写
引言:最近在工作中遇到与某些API对接的post的数据需要将对象的字段首字母小写.解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性 ...
- C#实体对象序列化成Json,格式化,并让字段的首字母小写
解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性) public class UserInfo { [JsonProperty(& ...
- C#调用webservice 时如何传递实体对象
在webservice端公开一个实体类,然后实例化,赋值,然后再给到webservice,可以实现,但是,即使调用端和service端的实体类完全一致,你也要重新实例化service端的,重新赋值,将 ...
- EBS OAF开发中实体对象和视图对象的属性设置器
EBS OAF开发中实体对象和视图对象的属性设置器 (版权声明.本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 源文: Home > Oracle ...
- 利用java反射将结果集封装成为对象和对象集合
java反射机制是什么 反射机制是在运行状态中,可以知道任何一个类的属性和方法,并且调用类的属性和方法: 反射机制能够做什么 1.判断运行对象的所属类 2.构造任意一个类的对象 3.获取任意一个类的属 ...
- 使用jackson工具类把对象或集合转为JSON格式
jackson使用方法: 1.加入jar包: jackson-annotations-2.2.2.jar jackson-core-2.2.2.jar jackson-databind-2.2.2.j ...
- DataTable转换为Model实体对象
记得在学校的时候,接触得最多的就是SqlHelper,每次在读取的时候不管是DataTable还是DataReader转换为实体对象的时候是最恼火的,因为要写很多代码,而且没有什么意义.后面接触到了反 ...
随机推荐
- codeigniter nginx rewrite规则配置【转】
转自:http://www.nginx.cn/1134.html nginx如何配置才能支持codeigniter ? 1. codeigniter的url美化去掉index.php 1 2 3 ...
- Foundation与coreFoundation的相互转换
今天在整理以前的一些琐碎知识,今天就分享一个Foundation与coreFoundation的相互转换细节问题,其中的引用计数器是需要考虑的方面. ARC 环境下,CoreFoundation框 ...
- thymelef 布局 fragment
需求:布局页面, 把首页分成四个页面: header footer ,content ,aside ,从githua 下载的原型, 所有内容是在一起的,这里拆分, 重用, 减少代码量 做法: 新建页 ...
- jbpm6 开发环境搭建
一.软件下载 1. eclipse 下载地址:http://www.eclipse.org/downloads/ 2.jbpm6.2 Installer 下载地址: ht ...
- 备战“软考”之软件project
说到"软件project"就有一种非常纠结的感觉!为什么呢?由于刚进入软考复习阶段,大家都把它放到了"比較"次要的地位,由于已经学过两遍,再加上它没有非常难理解 ...
- [D3 + AngularJS] 15. Create a D3 Chart as an Angular Directive
Integrating D3 with Angular can be very simple. In this lesson, you will learn basic integration as ...
- 开源免费天气预报接口API以及全国全部地区代码!!(国家气象局提供)
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- 卸载RedHat7自带的yum,安装并使用网易163源
由于redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,安装CentOS yum工具,再配置其他源,以下为详细过程: 删除redhat原有的yu ...
- Java 之文件IO编程 之读取
package com.sun; /* * 这里是对文件IO流读取的操作 * 2014-08-10 */ import java.io.*; public class File_test { publ ...
- GridView分页的实现
要在GridView中加入 //实现分页 AllowPaging="true" //一页数据10行 PageSize="10" // 分页时触发的事件 OnPa ...