/// <summary>
/// 隐含类型var 转换为 DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
ret.Columns.Add(dp.Name, dp.PropertyType);
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
Row[dp.Name] = dp.GetValue(item);
ret.Rows.Add(Row);
}
return ret;
}
}
c# 隐含类型var 转换为 DataTable
最新推荐文章于 2024-01-09 16:06:43 发布
文章介绍了如何使用C#编写的静态扩展方法,将泛型类型的IEnumerable转换为DataTable,通过遍历对象的属性并添加到DataTable中。
353

被折叠的 条评论
为什么被折叠?



