首页 > 学院 > 开发设计 > 正文

如何将List转换为DataTable

2019-11-17 03:14:06
字体:
来源:转载
供稿:网友

如何将List转换为DataTable

 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提供的反射.

(本文转载至互联网,抱歉找不到原链接,如有争议,请随时联系我删除)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表