• C#遍历/反射 属性/字段


    public static string SortParam<T>(T t)
            {
                string tStr = string.Empty;
                if (t == null)
                {
                    return string.Empty;
                }
                System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                if (properties.Length <= 0)
                {
                    return string.Empty;
                }
                foreach (System.Reflection.PropertyInfo item in properties.OrderBy(m => m.Name))//排序后组成字符串
                {
                    string name = item.Name;//字段名称
                    object value = item.GetValue(t, null);//值
                    if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                    {
                        if (value != null)
                            tStr += (string.Format("{0}{1}", name, value));
                    }
                    else
                    {
                        SortParam(value);//集合字段递归
                    }
                }
                //tStr = tStr.OrderBy(m => m).ToList();
                return tStr;
            }
    public IList<T> GetData(SqlDataReader reader)  
        {  
            IList<T> list = new List<T>();  
            Type type = typeof(T);  
            PropertyInfo[] properties = type.GetProperties();  
      
            while (reader.Read())  
            {  
                T t = Activator.CreateInstance<T>();  
                for (int i = 0; i < properties.Length; i++)  
                {  
                    properties[i].SetValue(t, reader[i + 1], null);  
      
                }  
      
                list.Add(t);  
            }  
      
            return list;  
        }
  • 相关阅读:
    print输出带颜色特效的方法
    为什么pymysql执行SQL语句后提示成功,但没有实际操作数据库?
    Mysql支持的数据类型
    基本的SQL语句
    初次运行Git前的配置
    git安装
    Hadoop HA 搭建
    Ubuntu下编译 Hadoop2.9
    Mac 下编译 Hadoop
    Windows下编译 Hadoop
  • 原文地址:https://www.cnblogs.com/Jerrycjc/p/5151704.html
Copyright © 2020-2023  润新知