public static List<T> ToList<T>(DataTable dt)
{
var list = new List<T>();
try
{
Type t = typeof(T);
var plist = new List<PropertyInfo>(typeof(T).GetProperties());
{
var list = new List<T>();
try
{
Type t = typeof(T);
var plist = new List<PropertyInfo>(typeof(T).GetProperties());
foreach (DataRow item in dt.Rows)
{
T s = System.Activator.CreateInstance<T>();
for (int i = 0; i < dt.Columns.Count; i++)
{
PropertyInfo info = plist.Find(p => p.Name.ToLower() == dt.Columns[i].ColumnName.ToLower());
if (info != null)
{
if (!Convert.IsDBNull(item[i]))
{
info.SetValue(s, item[i], null);
}
}
}
list.Add(s);
}
}
catch (Exception)
{
throw;
}
return list;
}
版权声明:本文为yddongzs原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。