1 public static DataTable ToDataTable<T>(List<T> list) 2 { 3 DataTable result = new DataTable(); 4 if (list.Count > 0) 5 { 6 PRopertyInfo[] propertys = list[0].GetType().GetProperties(); 7 foreach (PropertyInfo pi in propertys) 8 { 9 result.Columns.Add(pi.Name, pi.PropertyType);10 }11 12 for (int i = 0; i < list.Count; i++)13 {14 ArrayList tempList = new ArrayList();15 foreach (PropertyInfo pi in propertys)16 {17 object obj = pi.GetValue(list[i], null);18 tempList.Add(obj);19 }20 object[] array = tempList.ToArray();21 result.LoadDataRow(array, true);22 }23 }24 return result;25 }
重要:针对属性get set提供的反射.
(本文转载至互联网,抱歉找不到原链接,如有争议,请随时联系我删除)
新闻热点
疑难解答