All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET常用JS脚本整理_[Asp.Net教程]
C#网络应用编程基础练习题与答案(九)_[Asp.Net教程]
动态网页变静态:ASP.NET生成静态HTML页_.net资料_编程技术
用VB.NET,2005编写定时关机程序_.net资料_编程技术
如何用VB存取SQL,Server中的图像数据_[Asp.Net教程]
ASP.NET2.0,WebRource,开发微调按钮控件_[Asp.Net教程]
visual c++中基类与派生类概念
SQL Server 错误 ’80040e31’ 超时已过期_[SQL Server教程]
精通数据库系列之入门-技巧篇4_mssql学习_编程技术
asp.net连接mysql_[Asp.Net教程]
Reading number is top 10 pictures
何炅哥为中国人的平均工资鸣不平了
The goddess of the single reason2
南昌铁路局宜春车务段攸县车站铁路职工福利房被开发商侵占
9.3阅兵全景图1-抗战老兵和英雄连队梯队
The little girl with long hair2
The girl of like self-time
XuYing poker perspective garment debut
色狗系列
30 beautiful school beauty5
西班牙山村小景1
Download software ranking
Detective task-the top secret prostitution files
matrix1
Sora aoi, the nurse, uniform ,nursing assistant
VeryCD电驴(EasyMule) V1.1.9 Build09081
好色的外科大夫
Ashlynn Video4
jBuilder2006
Ashlynn Video5
I'm come from Beijing2
c#程序设计案例教程
delv published in(发表于) 2014/1/10 6:33:12 Edit(编辑)
通过反射填充泛型集合List的静态方法_[Asp.Net教程]

通过反射填充泛型集合List的静态方法_[Asp.Net教程]

通过反射填充泛型集合List的静态方法_[Asp.Net教程]

呃```花了一晚上时间,终于搞出来了如何通过反射,从DataReader将数据填充到数据实体泛型集合的静态方法.


//Kchen.Core.BaseBusinessObject为通用数据实体类,此处仅为限定T所继承的类型
public static IList FillDataListGeneric(System.Data.IDataReader reader) where T : Kchen.Core.BaseBusinessObject
{
//实例化一个List<>泛型集合
IList DataList = new List();
while (reader.Read())
{
//由于是是未知的类型,所以必须通过Activator.CreateInstance()方法来依据T的类型动态创建数据实体对象
T RowInstance = Activator.CreateInstance();
//通过反射取得对象所有的Property
foreach (PropertyInfo Property in typeof(T).GetProperties())
{
//BindingFieldAttribute为自定义的Attribute,用于与数据库字段进行绑定
foreach (BindingFieldAttribute FieldAttr in Property.GetCustomAttributes(typeof(BindingFieldAttribute), true))
{
try
{
//取得当前数据库字段的顺序
int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
if (reader.GetValue(Ordinal) != DBNull.Value)
{
//将DataReader读取出来的数据填充到对象实体的属性里
Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
}
}
catch
{
break;
}
}
}
//将数据实体对象add到泛型集合中
DataList.Add(RowInstance);
}
return DataList;
}
调用的时候使用如下代码


//伪代码 OleDbDataReader _ds = 创建一个OleDbDataReader
IList _result = Kchen.Utilities.FillDataListGeneric(_ds);


此静态方法通过一个实体类型和DateReader,快速的将数据填充到数据实体泛型集合中.


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.