会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
专注 .Net 寻找另类开发
抓住了就别放手,也不要后悔
转载:利用.NET中的反射机制实现IList到DataTable的转换
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
using
System.Collections;
using
System.Reflection;
namespace
KycBaseModule
{
public
class
KycFunction
{
public
KycFunction()
{ }
///
<summary>
///
实现对IList到DataSet的转换
///
</summary>
///
<param name="ResList">
待转换的IList
</param>
///
<returns>
转换后的DataSet
</returns>
public
static
DataSet ListToDataSet(IList ResList)
{
DataSet RDS
=
new
DataSet();
DataTable TempDT
=
new
DataTable();
//
此处遍历IList的结构并建立同样的DataTable
System.Reflection.PropertyInfo[] p
=
ResList[
0
].GetType().GetProperties();
foreach
(System.Reflection.PropertyInfo pi
in
p)
{
TempDT.Columns.Add(pi.Name,System.Type.GetType(pi.PropertyType.ToString()));
}
for
(
int
i
=
0
; i
<
ResList.Count; i
++
)
{
IList TempList
=
new
ArrayList();
//
将IList中的一条记录写入ArrayList
foreach
(System.Reflection.PropertyInfo pi
in
p)
{
object
oo
=
pi.GetValue(ResList[i],
null
);
TempList.Add(oo);
}
object
[] itm
=
new
object
[p.Length];
//
遍历ArrayList向object[]里放数据
for
(
int
j
=
0
; j
<
TempList.Count; j
++
)
{
itm.SetValue(TempList[j], j);
}
//
将object[]的内容放入DataTable
TempDT.LoadDataRow(itm,
true
);
}
//
将DateTable放入DataSet
RDS.Tables.Add(TempDT);
//
返回DataSet
return
RDS;
}
}
}
posted on
2009-02-03 23:08
Ооo酷鱼
阅读(
265
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
导航
博客园
首页
新随笔
订阅
管理
公告