zoukankan      html  css  js  c++  java
  • C#项目优化

    1.读取的封装

    public static List<T> ConvertData<T>(SqlDataReader sdr)
         {
             List<T> list = new List<T>();
             Type type = typeof(T);
             PropertyInfo[] properties = type.GetProperties();
             while (sdr.Read())
             {
                 T model = Activator.CreateInstance<T>();
                 for (int i = 0; i < properties.Length; i++)
                 {
                     for (int j = 0; j < sdr.FieldCount; j++)
                     {
                         //判断属性的名称和字段的名称是否相同
                         if (properties[i].Name == sdr.GetName(j))
                         {
                             Object value =sdr[j];
                             //将字段的值赋值给User中的属性
                             properties[i].SetValue(model, value, null);
                         }
                     }
                 }
                 list.Add(model);
             }
             return list;
         }
    
    //调用
    List<User> list = new List<User>();
    SqlDataReader sdr = cmd.ExecuteReader();
    list = ConvertData<User>(sdr);
    /// <summary>
            /// SqlDataReader读取封装对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="sdr"></param>
            /// <returns></returns>
            public static T ConvertObject<T>(SqlDataReader sdr)
            {
                Type type = typeof(T);
                PropertyInfo[] properties = type.GetProperties();
                T model = Activator.CreateInstance<T>();
                while (sdr.Read())
                {
                    for (int i = 0; i < properties.Length; i++)
                    {
                        for (int j = 0; j < sdr.FieldCount; j++)
                        {
                            //判断属性的名称和字段的名称是否相同
                            if (properties[i].Name == sdr.GetName(j))
                            {
                                Object value = sdr[j];
                                //将字段的值赋值给T中的属性
                                properties[i].SetValue(model, value, null);
                            }
                        }
                    }
                }
                return model;
            }
  • 相关阅读:
    HDU 1042 N!
    linux上安装wps办公软件
    《UNIX环境高级编程》笔记--环境变量
    【StatLearn】统计学习中knn算法实验(2)
    Asp.Net验证控件浅析
    斜率小于0的连线数量-归并排序
    Printk与sched_clock_init的一点分析
    【数字图像处理】使用kmeans算法对TrueColor图片进行优化
    将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
    python中os/sys/platform模块区别
  • 原文地址:https://www.cnblogs.com/hougelou/p/4023794.html
Copyright © 2011-2022 走看看