标签:class listt span ++ int table var tab for
1 private DataTable listToDataTable<T>(List<T> ListItem) 2 { 3 //实列化DataTable对象 4 var dt = new DataTable(typeof(T).Name); 5 6 //通过反射获取List实体的属性信息 7 PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 8 9 //给DataTable 添加定义 10 foreach (var propertyInfo in propertyInfos) 11 { 12 dt.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType); 13 } 14 15 //给DataTable 添加数据 16 foreach (T item in ListItem) 17 { 18 var values = new object[propertyInfos.Length]; 19 for (int i = 0; i < propertyInfos.Length; i++) 20 { 21 values[i] = propertyInfos[i].GetValue(item); 22 } 23 dt.Rows.Add(values); 24 } 25 26 //返回DataTable 27 return dt; 28 }
标签:class listt span ++ int table var tab for
原文地址:https://www.cnblogs.com/YourDirection/p/12106201.html