将DataTable集合反射获取 List<M>

 /// <summary>
/// 根据DataTable集合反射获取 List<M>
/// </summary>
/// <typeparam name="M">泛型实体</typeparam>
/// <param name="dt">DataTable</param>
/// <returns>实体集合</returns>
private static List<M> SetValueRow<M>(DataTable dt) where M : new()
{
List<M> list = new List<M>(); Type type;
PropertyInfo p;
M m; foreach (DataRow row in dt.Rows)
{
m = new M();
type = m.GetType(); foreach (DataColumn col in dt.Columns)
{
//获取一个字段的属性
p = type.GetProperty(col.ColumnName); //实体中无对应属性
if (p == null)
continue; string colDbType = row[col.ColumnName].GetType().FullName; //结果集单元格中的值不为空时才赋值
if (colDbType != "System.DBNull")
{
switch (p.PropertyType.FullName)
{
case "System.Int64"://根据不同数据库数据类型作转换,如oracle的number(2)应转换为Int32,而不是默认的Decemal
p.SetValue(m, Convert.ToInt64(row[col.ColumnName]), null);
break; case "System.Int32":
p.SetValue(m, Convert.ToInt32(row[col.ColumnName]), null);
break; case "System.Int16":
p.SetValue(m, Convert.ToInt16(row[col.ColumnName]), null);
break; case "System.String":
p.SetValue(m, Convert.ToString(row[col.ColumnName]), null);
break; case "System.Decimal":
p.SetValue(m, Convert.ToDecimal(row[col.ColumnName]), null);
break; case "System.DateTime":
p.SetValue(m, row[col.ColumnName], null);
break; case "System.Double":
p.SetValue(m, Convert.ToDouble(row[col.ColumnName]), null);
break; case "System.Boolean":
p.SetValue(m, Convert.ToBoolean(row[col.ColumnName]), null);
break; case "System.Byte":
p.SetValue(m, Convert.ToByte(row[col.ColumnName]), null);
break; default:
p.SetValue(m, row[col.ColumnName], null);
break;
}
}
} list.Add(m);
} return list;
}

将IList集合类转换成DataTable

/// <summary>
/// 将IList集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable IListToDataTable(IList list)
{
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
将List<M>集合类转换成DataTable 
        /// <summary>
/// 将List<M>集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable IListToDataTable<M>(List<M> list)
{
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
全局静态类 泛型方法
/// <summary>
/// 全局静态类
/// </summary>
public static class GlobalStaticClass : Object
{ public static List<M> ToModelList<M>(this object obj) where M : new()
{
List<M> list = new List<M>(); Type type;
PropertyInfo p;
M m;
DataTable dt = (DataTable)obj; foreach (DataRow row in dt.Rows)
{
m = new M();
type = m.GetType(); foreach (DataColumn col in dt.Columns)
{
//获取一个字段的属性
p = type.GetProperty(col.ColumnName); //实体中无对应属性
if (p == null)
continue; string colDbType = row[col.ColumnName].GetType().FullName; //结果集单元格中的值不为空时才赋值
if (colDbType != "System.DBNull")
{
switch (p.PropertyType.FullName)
{
case "System.Int64"://根据不同数据库数据类型作转换,如oracle的number(2)应转换为Int32,而不是默认的Decemal
p.SetValue(m, Convert.ToInt64(row[col.ColumnName]), null);
break; case "System.Int32":
p.SetValue(m, Convert.ToInt32(row[col.ColumnName]), null);
break; case "System.Int16":
p.SetValue(m, Convert.ToInt16(row[col.ColumnName]), null);
break; case "System.String":
p.SetValue(m, Convert.ToString(row[col.ColumnName]), null);
break; case "System.Decimal":
p.SetValue(m, Convert.ToDecimal(row[col.ColumnName]), null);
break; case "System.DateTime":
p.SetValue(m, row[col.ColumnName], null);
break; case "System.Double":
p.SetValue(m, Convert.ToDouble(row[col.ColumnName]), null);
break; case "System.Boolean":
p.SetValue(m, Convert.ToBoolean(row[col.ColumnName]), null);
break; case "System.Byte":
p.SetValue(m, Convert.ToByte(row[col.ColumnName]), null);
break; default:
p.SetValue(m, row[col.ColumnName], null);
break;
}
}
} list.Add(m);
} return list;
} }

实体父类,实体继承此类后,实体对象可调用this.SetValue(object) 方法通过反射给自身对象赋值

    public class ModelBase
{
protected bool isNull = true;
public bool IsNull
{
get { return isNull; }
set { isNull = value; }
}
protected void SetValue(object info)
{
foreach (FieldInfo fi in info.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
this.GetType().GetField(fi.Name, BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, fi.GetValue(info));
}
}
public void SetValue(SqlDataReader dr)
{
if (dr.Read())
{
foreach (FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (fi.Name != "isNull")
{
object rel = dr[fi.Name];
if (dr[fi.Name] != Convert.DBNull)
{
fi.SetValue(this, dr[fi.Name]);
} }
}
this.isNull = false;
}
dr.Close();
}
public void SetValue(DataRow dr)
{
foreach (FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (fi.Name != "isNull")
{
if (dr.Table.Columns.Contains(fi.Name))
{
object rel = dr[fi.Name];
if (dr[fi.Name] != Convert.DBNull)
{
fi.SetValue(this, dr[fi.Name]);
}
}
}
}
this.isNull = false; } }

反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>的更多相关文章

  1. c# 反射得到实体类的字段名称和值,DataTable转List<T>

    /// <summary> /// 反射得到实体类的字段名称和值 /// var dict = GetProperties(model); /// </summary> /// ...

  2. 完整DataTable与IList互换(转)

    public class CollectionHelper { private CollectionHelper() { } public static DataTable ConvertTo< ...

  3. Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader

    前文介绍了Winform为DataGridView提供的数据自动绑定功能,下面介绍一下采用代码的数据绑定 1.用DataSet和DataTable为DataGridView提供数据源 先上代码 pri ...

  4. 【2017001】IList转DataTable、DataTable转IList

    IList转DataTable.DataTable转IList using System; using System.Collections.Generic; using System.Compone ...

  5. Java反射获取class对象的三种方式,反射创建对象的两种方式

    Java反射获取class对象的三种方式,反射创建对象的两种方式 1.获取Class对象 在 Java API 中,提供了获取 Class 类对象的三种方法: 第一种,使用 Class.forName ...

  6. C#之DataTable转List与List转Datatable

    闲来无事,只有写代码啦,以下为DataTable转List与List转DataTable的两个方法,主要技术点用到了反射原理: /// <summary> /// 模型转换类 /// &l ...

  7. C# DataTable转List And List转DataTable

    // DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...

  8. “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用

    “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用 造成这个错误的原因是,在 ...

  9. 多个不同的表合并到一个datatable中,repeater在绑定datatable

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  10. 将两个列不同的DataTable合并成一个新的DataTable

    /// <summary>         /// 将两个列不同(结构不同)的DataTable合并成一个新的DataTable         /// </summary> ...

随机推荐

  1. 交叉编译python2.7.7

    一.python下载网址: http://www.python.org/ftp/python/ 二.python的交叉编译依赖openssl ,openssl的下载网址: https://www.op ...

  2. [z]分区truncate操作的介绍及对全局索引和空间释放影响的案例解析

    [z]https://www.2cto.com/database/201301/181226.html 环境: [sql] [oracle@localhost ~]$ uname -r 2.6.18- ...

  3. vue缓存之keep-alive,设置想要缓存的页面

    由于项目需求从a页面跳转到b页面,返回a页面,a页面数据不能被刷新掉,方法很多列举12 方法1 a页面通过学期按钮切换学期,该学期里more进入b页面,返回a页面,返回回到对应a页面进入的高亮按钮设置 ...

  4. python 面向对象编程 之 反射

    1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...

  5. 语法分析器初步学习——LISP语法分析

    语法分析器初步学习——LISP语法分析 本文参考自vczh的<如何手写语法分析器>. LISP的表达式是按照前缀的形式写的,比如(1+2)*(3+4)在LISP中会写成(*(+ 1 2)( ...

  6. Carbon document

    <   Getting Started Docs Reference History Contribute Github Introduction The Carbon class is inh ...

  7. Netty 源码 Channel(一)概述

    Netty 源码 Channel(一)概述 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) Channel 为 Netty ...

  8. SSRF-php初探

    0x00 前言 1)    SSRF的概念很好理解,请自行百度. 2)    JAVA/PHP/PYTHON都存在SSRF漏洞(至于其他语言的情况,了解粗浅尚不得知). 3)    SSRF的利用方式 ...

  9. Trapping Rain Water LT42

    The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of ...

  10. android 打开新窗口

    ImageView loginBtn = (ImageView)findViewById(R.id.login_button); loginBtn.setOnClickListener(new Vie ...