1         protected List<T> ConvertDataTableToTargetTypeList<T>(DataTable table)
 2         {
 3             List<T> list = new List<T>();
 4             Type typeInfo = typeof(T);
 5             //得到T内所有的公共属性
 6             PropertyInfo[] propertys = typeInfo.GetProperties();
 7             foreach (DataRow rowItem in table.Rows)
 8             {
 9                 //通过反射动态创建对象
10                 T objT = Activator.CreateInstance<T>();
11                 //给objT的所有属性赋值
12                 foreach (DataColumn columnItem in table.Columns)
13                 {
14                     foreach (PropertyInfo property in propertys)
15                     {
16                         if (columnItem.ColumnName.ToLower().Equals(property.Name.ToLower()))
17                         {
18                             //获取指定单元格的值
19                             object value = rowItem[columnItem.ColumnName];
20                             if (value != DBNull.Value)
21                             {
22                                 property.SetValue(objT, value, null);
23                             }
24                             break;
25                         }
26                     }
27                 }
28                 list.Add(objT);
29             }
30             return list;
31         }