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 = 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;
        }

 

posted @ 2015-01-07 15:36  王小贝  阅读(360)  评论(0编辑  收藏  举报