public List<T> TableToList<T>(FX.Data.DataTable dt) where T: new()
{
// 定义集合
IList<T> list = new List<T>();
// 获得此模型的类型
Type type = typeof(T);
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue;
object value = dr[pi.Name];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
list.Add(t);
}
return list.ToList();
}
DataTable转List
最新推荐文章于 2024-07-31 23:35:31 发布