DataSet 反射转换成 List<T>
/// <summary> /// DataSet转换成指定返回类型的实体集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ds"></param> /// <returns></returns> public static List<T> DataSetToList<T>(DataSet ds) { PropertyInfo[] properties = typeof(T).GetProperties(); List<T> list = new List<T>(); foreach (DataRow dr in ds.Tables[0].Rows) { T temp = System.Activator.CreateInstance<T>(); foreach (PropertyInfo pro in properties) { int colIndex = ds.Tables[0].Columns.IndexOf(pro.Name); if (colIndex > -1) { if (pro.PropertyType == typeof(String)) { pro.SetValue(temp, dr[colIndex].ToString(), null); } else if (pro.PropertyType == typeof(int)) { pro.SetValue(temp, (int)dr[colIndex], null); } else if (pro.PropertyType == typeof(long)) { pro.SetValue(temp, (long)dr[colIndex], null); } } } list.Add(temp); } return list; }