将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. IIS挂起网站配置文件地址

    “C/用户/Administrator/我的文档/IISExpress/Config/applicationhost”

  2. 设置Tomcat的JAVA_OPTS参数

    修改$TOMCAT_HOME/bin/catalina.bat 添加set JAVA_OPTS= ... rem ----- Execute The Requested Command ------- ...

  3. bean生命周期_Junit测试使用factory模式

    在面试某互联网_保险 公司, 被问到了spring中bean的生命周期,不禁联想到我们之前作 junit测试时,使用了Factory模式,而没有用Mock.

  4. 使用JFinal实现使用MVC获取表单中的数据并将提示信息返回给另一jsp页面。

    1.包结构 2.我们需要对web.xml进行配置: <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  5. centos 命令学习

    关机&重启 shutdown -h 10          #计算机将于10分钟后关闭,且会显示在登录用户的当前屏幕中 shutdown -h now       #计算机会立刻关机 shut ...

  6. Oracle_PL/SQL(3) 游标

    引言:PLSQL数据类型标量数据类型:数字类.字符类.日期类.布尔类(boolean).复合数据类型:记录(%rowtype).表.数组引用类型:REF CURSORLOB类型:BLOB.CLOB 1 ...

  7. 如何在Fragment中获取context

    文章转载自http://blog.csdn.net/demonliuhui/article/details/51511136 这里仅供自己学习参考: Context,中文直译为“上下文”,SDK中对其 ...

  8. 28335XINTF的简单使用

    28335 XINTF基本特点 一共有三个外部存储区域:区域0(Zone 0),区域6(Zone 6)和区域7(Zone 7).对应的 访问地址为:Zone 0:0x0000_4000-0x0000_ ...

  9. RNA测序相对基因表达芯片有什么优势?

    RNA测序相对基因表达芯片有什么优势? RNA-Seq和基因表达芯片相比,哪种方法更有优势?关键看适用不适用.那么RNA-Seq适用哪些研究方向?是否您的研究?来跟随本文了解一下RNA测序相对基因表达 ...

  10. SystemTap 工作原理

    <systemtap原理及使用> https://www.cnblogs.com/youngerchina/p/5624588.html 这篇帖子前边系统介绍了systemtap的工作原理 ...