1 /// <summary>
2 /// 将集合类转换成DataTable
3 /// </summary>
4 /// <param name="list">集合</param>
5 /// <returns></returns>
6 public static DataTable ToDataTable(IList list)
7 {
8 DataTable result = new DataTable();
9 if (list.Count > 0)
10 {
11 PropertyInfo[] propertys = list[0].GetType().GetProperties();
12 foreach (PropertyInfo pi in propertys)
13 {
14 result.Columns.Add(pi.Name, pi.PropertyType);
15 }
16
17 for (int i = 0; i < list.Count; i++)
18 {
19 ArrayList tempList = new ArrayList();
20 foreach (PropertyInfo pi in propertys)
21 {
22 object obj = pi.GetValue(list[i], null);
23 tempList.Add(obj);
24 }
25 object[] array = tempList.ToArray();
26 result.LoadDataRow(array, true);
27 }
28 }
29 return result;
30 }