DavidYun

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
publicstatic DataSet ConvertToDataSet<T>(IList<T> list)
{
if (list ==null|| list.Count <=0)
{
returnnull;
}
DataSet ds
=new DataSet();
DataTable dt
=new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;

System.Reflection.PropertyInfo[] myPropertyInfo
=typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t ==null)
{
continue;
}
row
= dt.NewRow();
for (int i =0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi
= myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] ==null)
{
column
=new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name]
= pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
}

  

posted on 2011-09-06 12:56  David.Yun  阅读(1910)  评论(0编辑  收藏  举报