zoukankan      html  css  js  c++  java
  • DataSet 反射转换成 List<T>

            /// <summary>
            /// DataSet转换成指定返回类型的实体集合
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="ds"></param>
            /// <returns></returns>
            public static List<T> DataSetToList<T>(DataSet ds)
            {
                PropertyInfo[] properties = typeof(T).GetProperties();
                List<T> list = new List<T>();
    
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    T temp = System.Activator.CreateInstance<T>();
                    foreach (PropertyInfo pro in properties)
                    {
                        int colIndex = ds.Tables[0].Columns.IndexOf(pro.Name);
                        if (colIndex > -1)
                        {
                            if (pro.PropertyType == typeof(String))
                            {
                                pro.SetValue(temp, dr[colIndex].ToString(), null);
                            }
                            else if (pro.PropertyType == typeof(int))
                            {
                                pro.SetValue(temp, (int)dr[colIndex], null);
                            }
                            else if (pro.PropertyType == typeof(long))
                            {
                                pro.SetValue(temp, (long)dr[colIndex], null);
                            }
                        }
                    }
                    list.Add(temp);
                }
                return list;
            }
  • 相关阅读:
    消息机制
    窗口!窗口!- Windows程序设计(SDK)003
    内联函数的作用
    结构体变量用 . 结构体指针用-> 的原因
    &a和a的区别
    分布电容
    介电常数
    天线
    封装的思想
    关于中断标志位
  • 原文地址:https://www.cnblogs.com/woadmin/p/9406788.html
Copyright © 2011-2022 走看看