using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Reflection; namespace Extension { public static class Extension { public static IList<T> ToList<T>(this DataTable dt) { var lst = new List<T>(); var plist = new List<System.Reflection.PropertyInfo>(typeof(T).GetProperties()); foreach (DataRow item in dt.Rows) { T t = System.Activator.CreateInstance<T>(); for (int i = 0; i < dt.Columns.Count; i++) { PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName); if (info != null) { if (!Convert.IsDBNull(item[i])) { info.SetValue(t, item[i], null); } } } lst.Add(t); } return lst; ///throw new NotImplementedException(); } } }
DataTable dt = DAL.DAL.gettable();
var u = dt.ToList<user>();
http://www.cnblogs.com/jasonxuvip/archive/2012/08/03/2621674.html
时间: 2024-12-29 12:33:13