反射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> ...
随机推荐
- cpio解压initramfs.img
一.解压initramfs.img # mkdir test # cp /boot/initramfs.img /test # cd test # file initramfs.img initram ...
- linux arm-linux-gcc 安装编译
1,将 .tgz 安装包通过SSH传至ubuntu 2,tar -zxvf arm-linux-gcc.tgz 解压 3,配置环境变量(由于鄙人只需其中一个用户使用,所以直接再其主目录) ...
- .netcore webapi 在startup中读取配置字符串
参考微软官方说明:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration 具体为: 读取方法见下图中标红的格式, ...
- 洛谷3084 [USACO13OPEN]照片Photo
原题链接 神仙\(DP\)啊... 题解请移步隔壁大佬的博客\(QAQ\) #include<cstdio> using namespace std; const int N = 2e5 ...
- 在nginx中,禁止IP访问.只可以使用域名访问.
if ($host ~* "\d+\.\d+\.\d+\.\d+"){ ; } 其实说白了, 就是进行host主机头过滤,使用正则来判断下.
- [线段树]picture
PICTURE 题目描述 N(N<5000) 张矩形的海报,照片和其他同样形状的图片贴在墙上.它们的边都是垂直的或水平的.每个矩形可以部分或者全部覆盖其他矩形.所有的矩形组成的集合的轮廓称为周长 ...
- 杭电1133 排队买票 catalan
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- pycharm 创建文件时,自动添加文件头注释
File->settings->Editor->File and Code Templates->Python Script # -*- coding: utf-8 -*- & ...
- Activiti任务认领
Activiti任务认领 TaskService taskService; taskService.setAssignee(String taskId, String userId);taskServ ...
- tomcat运行监控脚本,自动启动
参见:http://www.cnblogs.com/coffee_cn/p/8279165.html monitor.sh #!/bin/sh monitorlog=/usr/local/tomcat ...