62,074
社区成员
发帖
与我相关
我的任务
分享
//抄人家的代码
public IList<T> GetList<T>(DataTable table)
{
IList<T> list = new List<T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance<T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
object value = row[tempName];
pro.SetValue(t, value, null);
}
}
list.Add(t);
}
return list;
}