List转换成DataSet实现代码_C#教程-查字典教程网
List转换成DataSet实现代码
List转换成DataSet实现代码
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下://////List转换成DataSet//////类型///将要转换的List///publicDataSetConve...

复制代码 代码如下:

/// <summary>

/// List转换成DataSet

/// </summary>

/// <typeparam name="T">类型</typeparam>

/// <param name="list">将要转换的List</param>

/// <returns></returns>

public DataSet ConvertToDataSet<T>(IList<T> list)

{

if (list == null || list.Count <= 0)

{

return null;

}

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;

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类