反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>
将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>的更多相关文章
- c# 反射得到实体类的字段名称和值,DataTable转List<T>
/// <summary> /// 反射得到实体类的字段名称和值 /// var dict = GetProperties(model); /// </summary> /// ...
- 完整DataTable与IList互换(转)
public class CollectionHelper { private CollectionHelper() { } public static DataTable ConvertTo< ...
- Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader
前文介绍了Winform为DataGridView提供的数据自动绑定功能,下面介绍一下采用代码的数据绑定 1.用DataSet和DataTable为DataGridView提供数据源 先上代码 pri ...
- 【2017001】IList转DataTable、DataTable转IList
IList转DataTable.DataTable转IList using System; using System.Collections.Generic; using System.Compone ...
- Java反射获取class对象的三种方式,反射创建对象的两种方式
Java反射获取class对象的三种方式,反射创建对象的两种方式 1.获取Class对象 在 Java API 中,提供了获取 Class 类对象的三种方法: 第一种,使用 Class.forName ...
- C#之DataTable转List与List转Datatable
闲来无事,只有写代码啦,以下为DataTable转List与List转DataTable的两个方法,主要技术点用到了反射原理: /// <summary> /// 模型转换类 /// &l ...
- C# DataTable转List And List转DataTable
// DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...
- “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用
“DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用 造成这个错误的原因是,在 ...
- 多个不同的表合并到一个datatable中,repeater在绑定datatable
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 将两个列不同的DataTable合并成一个新的DataTable
/// <summary> /// 将两个列不同(结构不同)的DataTable合并成一个新的DataTable /// </summary> ...
随机推荐
- FTPserver
客户端代码: import os import hashlib BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__) ...
- ubuntu关闭服务需要身份验证
service tomcat stop ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === 需要通过认证才能停止“tom ...
- 移动端页面返回,数据不刷新bug解决
一,当安卓和ios都有问题的时候 // a.html 设置刷新 检测缓存是否有标志 要是有就说明数据有变化 a.html跳转到b.html页面 window.addEventListener(&quo ...
- vue的通讯与传递props emit (简单的弹框组件)
props父把信息传递给子组件 1父组件 <template> <div class="hello"> <div id="app-3&quo ...
- JAVA动手动脑及课后作业
1.查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 运行结果 true true false 原因 1)在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0, ...
- Android.PublishApplication
发布应用 3. 为App签名 Android 要求App在安装前,需要使用证书(certificate)来进行数字签名(be digitally signed). Android 用证书来标识一个Ap ...
- (O)web缓存
为什么要用缓存 一般针对静态资源如CSS,JS,图片等使用缓存,原因如下: 请求更快:通过将内容缓存在本地浏览器或距离最近的缓存服务器(如CDN),在不影响网站交互的前提下可以大大加快网站加载速度. ...
- CH6202 黑暗城堡
一道最短路+生成树 原题链接 实际上就是生成树的中每个点到节点\(1\)的距离等于原图中这个点到节点\(1\)的最短距离,求这样的生成树的棵数. 先用\(SPFA\)或\(Dijkstra\)求出所有 ...
- 互斥量mutex的简单使用
几个重要的函数: #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pt ...
- xcode如何运行下载的demo工程
1. 首先你需要改Bundle Identifier,修改成别人没注册过的. 2. 选中 “Automatically manage signing” 3. 在Team里面添加自己的个人帐号了.