c#將list類型轉換成DataTable方法示例
更新時間:2013年12月26日 09:51:36 作者:
將List類型轉換成DataTable的通用方法,大家參考使用吧
復制代碼 代碼如下:
/// <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;
}
相關文章
Unity UGUI的RectMask2D遮罩組件的介紹使用
這篇文章主要為大家介紹了Unity UGUI的RectMask2D遮罩組件的介紹使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

