using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.ComponentModel;
namespace SmartERP.Until
{
public static class CovertClass
{
/// <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;
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dtlcq/archive/2009/10/22/4713101.aspx