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

Reading number is top 10 articles
ASP.NET常用路径(path)获取方法对照表_[Asp.Net教程]
创建索引对SQL语句执行的影响_[SQL,Server教程]
[delphi语法1]Object Pascal语言编写环境
详谈基于JSON的高级AJAX开发技术_.net资料_编程技术
WCF分布式开发常见错误:-Unrecognized,attribute,’targetFramework’,(未识别的属性’targetFramework’,)_.net资料_编程技术
获取Web.Config文件中AppSetting_[Asp.Net教程]
详细介绍动态网页PHP预定义变量之服务器变量_php资料_编程技术
ASP.NET生成静态HTML页面并分别按年月目录存放_[Asp.Net教程]
JavaScript与XML XSL的综合使用_[XML教程]
用javascript轻松制作抽奖系统_JavaScript技术_编程技术
Reading number is top 10 pictures
The Soviet union swimsuit exposure in the 70 year2
A cat have life principles
湖边的风景
Japanese snow monkeys in the hot spring to keep warm, close their eyes to enjoy
The most popular girls welcome eggplant
Sora aoi in China3
So beauty, will let you spray blood10
传销的好处
银行20年后可能消失
红楼梦金陵十二钗(2)
Download software ranking
Unix video tutorial8
Unix video tutorial19
少妇苏霞全本
Take off clothes to survival
Proficient in JavaScript
Unix video tutorial15
SP3 for SQL2000
Unix video tutorial20
Unix video tutorial6
Unix video tutorial3
aaa published in(发表于) 2013/12/11 8:31:24 Edit(编辑)
通过反射填充泛型集合List的静态方法_.net资料_编程技术

通过反射填充泛型集合List的静态方法_.net资料_编程技术

通过反射填充泛型集合List的静态方法_.net资料_编程技术-你的首页-uuhomepage.com

呃```花了一晚上时间,终于搞出来了如何通过反射,从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.