private static list<t> tabletoentity<t>(datatable dt) where t : class,new()
{
type type = typeof(t);
list<t> list = new list<t>();
foreach (datarow row in dt.rows)
{
propertyinfo[] parray = type.getproperties();
t entity = new t();
foreach (propertyinfo p in parray)
{
if (row[p.name] is int64)
{
p.setvalue(entity, convert.toint32(row[p.name]), null);
continue;
}
p.setvalue(entity, row[p.name], null);
}
list.add(entity);
}
return list;
}
// 调用:
list<user> userlist = tabletoentity<user>(yourdatatable);
网友评论