zoukankan      html  css  js  c++  java
  • 利用反射将Datatable转为List集合

    public List<T> Show1<T>(DataTable dt)where T : class, new()
            {
                List<T> list = new List<T>();
                string ss = string.Empty;
                //获取类型
                Type tp = typeof(T);
                //遍历数据行
                foreach (DataRow item in dt.Rows)
                {
                    T t = new T();
                    //获取所有的公共属性
                    PropertyInfo[] properties = tp.GetProperties();
                    //遍历属性
                    foreach (PropertyInfo aa in properties)
                    {
                        //将属性名赋给变量
                        ss = aa.Name;
                        //
                        if (dt.Columns.Contains(ss))
                        {
                            //获取属性名所对应的值
                            object value = item[ss];
                            if (value != DBNull.Value)
                            {
                                //赋值
                                aa.SetValue(t, value, null);
                            }
                        }
                    }
                    list.Add(t);
                }
                dt.Clear();
                dt.Dispose();
                return list;
            }
  • 相关阅读:
    git
    build and set proxy in Ubuntu
    export a java project to runable jar
    Remove openjdk in Ubuntu/Configure jdk and running adb in 64-bit Ubuntu
    When you install printer in Ubuntu, just need a ppd file.
    Ubuntu user switch
    Enable SSHD on Ubuntu
    web测试实践——day01
    白盒测试实践-day04
    白盒测试实践-day03
  • 原文地址:https://www.cnblogs.com/sange118/p/13139176.html
Copyright © 2011-2022 走看看