C# 将DataTable装换位List 泛型

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 = 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> 泛型

上一篇:C#—总结


下一篇:【百度地图API】如何获取行政区域的边界?