标签:des style class ext width color get int cti rgb line
public
static class ExtensionMethods
{
///
<summary>
///
将List转换成DataTable
///
</summary>
///
<typeparam name="T"></typeparam>
///
<param name="data"></param>
///
<returns></returns>
public
static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection
properties = TypeDescriptor.GetProperties(typeof(T));
DataTable
dt = new DataTable();
for
(int i = 0; i < properties.Count; i++)
{
PropertyDescriptor
property = properties[i];
dt.Columns.Add(property.Name,
property.PropertyType);
}
object[]
values = new object[properties.Count];
foreach
(T item in data)
{
for
(int i = 0; i < values.Length; i++)
{
values[i]
= properties[i].GetValue(item);
}
dt.Rows.Add(values);
}
return
dt;
}
}
c#将list集合转换为datatable的简单办法,码迷,mamicode.com
标签:des style class ext width color get int cti rgb line
原文地址:http://www.cnblogs.com/zuozongyao/p/3701468.html