public class DatatableToModelHelper<T> where T : new()
{
public static IList<T> ConvertToModel(System.Data.DataTable dt)
{
IList<T> ts = new List<T>();
Type type = typeof(T);
string tempName = "";
foreach (System.Data.DataRow dr in dt.Rows)
{
T t = new T();
System.Reflection.PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo pi in propertys)
{
tempName = pi.Name;
if (dt.Columns.Contains(tempName))
{
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
Reflection Datatable To Model
最新推荐文章于 2021-02-02 12:00:10 发布