将row中的内容一个个的拿出来,放到对象中
public static void SetValue<T>(this T obj, DataRow dr)
{
foreach (DataColumn dc in dr.Table.Columns)
{
var columnName = dc.ColumnName;
if (dr[dc] == DBNull.Value)
continue;
if (!GetObjectPropertyInfo<T>.IsContainsProperty(columnName))
continue;
var property = GetObjectPropertyInfo<T>.GetPropertyInfo(columnName);
try
{
property.SetValue(obj, dr[dc], null);
}
catch (Exception ex)
{
throw new Exception("Can not set object property (" + columnName + ") in ReflectionSetValue(target object type:" + typeof(T).ToString() + ", " + ex.Message + ")");
}
}
}
public static List<T> ToList<T>(this DataTable dt) where T : new()
{
var list = new List<T>();
foreach (DataRow dr in dt.Rows)
{
var obj = new T();
obj.SetValue(dr);
list.Add(obj);
}
return list;
}