首页 > 代码库 > C# 将DataTable装换位List<T> 泛型

C# 将DataTable装换位List<T> 泛型

public List<T> GetList<T>(DataTable dt) where T:new()        {            List<T> DateLists = new List<T>();            string Typename = "";            foreach (DataRow rows in dt.Rows)            {                T t = new T();                PropertyInfo[] info = typeof(T).GetProperties();                foreach (PropertyInfo pi in info)                {                    Typename = pi.Name;                    if (dt.Columns.Contains(Typename))                    {                        object value =http://www.mamicode.com/ rows[Typename];                         if (value!=DBNull.Value)                        {                            if (pi.PropertyType.ToString()=="System.String")                            {                                pi.SetValue(t, System.Web.HttpUtility.HtmlDecode(value.ToString()), null);                            }                            else                            {                                pi.SetValue(t,value,null);                            }                        }                    }                }                DateLists.Add(t);            }            return DateLists;        }

 

C# 将DataTable装换位List<T> 泛型