将datatable转换成模型
2016-03-18 17:43
204 查看
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RongGuang.Extend
{
class ModelConvertHelper where T : new()
{
public static IList ConvertToModel(DataTable dt)
{
// 定义集合
IList ts = new List();
}
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RongGuang.Extend
{
class ModelConvertHelper where T : new()
{
public static IList ConvertToModel(DataTable dt)
{
// 定义集合
IList ts = new List();
// 获得此模型的类型 Type type = typeof(T); string tempName = ""; foreach (DataRow dr in dt.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } }
}
相关文章推荐
- Powerdesigner 16 各种模型转换
- ATL模型转换技术详解
- iOS 字典转模型
- 安卓检查APP是否被用户拒绝调用摄像头和麦克风权限
- freeradius+mysql+交换机认证
- 【转】android布局--Android fill_parent、wrap_content和match_parent的区别
- 关于开放性跳转的一个安全小问题
- 文章标题
- 使用自定义Adapter的ListView优化方式
- Handler
- 深度卷积网络CNN与图像语义分割
- Java并发 线程池
- day⑧:socket 和socketserver
- Git配置和一些常用命令
- C++虚继承(七) --- 虚继承对基类构造函数调用顺序的影响
- enum枚举类型详解
- HAproxy指南之haproxy重定向应用(案例篇)
- Spring MVC里面的预防 SQL 注入
- 静态链表的实现
- Oracle 数据导出工具 Spool