zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    SQLI DUMB SERIES-12
    SQLI DUMB SERIES-11
    SQLI DUMB SERIES-9&&10
    SQLI DUMB SERIES-8
    SQLI DUMB SERIES-7
    XXS level10
    XXS level9
    XXS level8
    XXS level7
    XXS level6
  • 原文地址:https://www.cnblogs.com/xiao-bei/p/4208595.html
Copyright © 2011-2022 走看看