public DataTable IListOut(IList<T> ResList)
{
DataTable TempDT = new DataTable();
//此處遍歷IList的結構並建立同樣的DataTable
System.Reflection.PropertyInfo[] p = ResList[0].GetType().GetProperties();
foreach (System.Reflection.PropertyInfo pi in p)
{
TempDT.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
}
for (int i = 0; i < ResList.Count; i++)
{
IList TempList = new ArrayList();
//將IList中的一條記錄寫入ArrayList
foreach (System.Reflection.PropertyInfo pi in p)
{
object oo = pi.GetValue(ResList[i], null);
TempList.Add(oo);
}
object[] itm = new object[p.Length];
//遍歷ArrayList向object[]裏放數據
for (int j = 0; j < TempList.Count; j++)
{
itm.SetValue(TempList[j], j);
}
//將object[]的內容放入DataTable
TempDT.LoadDataRow(itm, true);
}
//返回DataTable
return TempDT;
}