1、Table转List
/// <summary>
/// Table转List
/// </summary>
/// <typeparam name="T"></typeparam>
public class GenericList<T> : List<T>
{
public GenericList(DataTable dt, Type tt)
{
object ff = Activator.CreateInstance(tt, null);//创建指定类型实例
PropertyInfo[] fields = ff.GetType().GetProperties();//获取指定对象的所有公共属性
foreach (DataRow dr in dt.Rows)
{
object obj = Activator.CreateInstance(tt, null);
foreach (DataColumn dc in dt.Columns)
{
var dc1 = dc;
foreach (PropertyInfo t in fields.Where(t => dc1.ColumnName.ToLower().Equals(t.Name.ToLower())))
{
var value = dr[dc.ColumnName];
if (t.PropertyType.Name.ToLower() == "int32" && dr[dc.ColumnName].ToString().Length < 1)
value = 0;
t.SetValue(obj, Convert.ChangeType(value, t.PropertyType), null); //给对象赋值
continue;
}
}
Add((T)obj);//将对象填充到list集合
}
}
}/// <summary>
/// Tab