- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Collections;
- using System.Reflection;
- namespace DatableToList
- {
- class ConvertHelper<T> where T : new()
- {
-
-
-
-
-
- public static List<T> ConvertToList(DataTable dt)
- {
-
-
- List<T> ts = new List<T>();
-
-
- Type type = typeof(T);
-
- string tempName = string.Empty;
-
- foreach (DataRow dr in dt.Rows)
- {
- T t = new T();
-
- PropertyInfo[] propertys = t.GetType().GetProperties();
-
- foreach (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;
-
- }
- }
- }
DataTable转换成List<T>