zoukankan      html  css  js  c++  java
  • 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;  
        }
  • 相关阅读:
    [Quote] Android Graphics Architecture
    New NFC log
    [Quote] 3.6 Namespaces
    NFC DAL(Driver Abstraction Layer) and OSAL(Operating System Abstraction Layer)
    Finance&Invest&Economics URL Links
    Concepts
    Tracking NFC Flow
    NFC log
    [IoE] About AllJoyn™
    [Quote] How does getSystemService() work exactly?
  • 原文地址:https://www.cnblogs.com/Jerrycjc/p/5151704.html
Copyright © 2011-2022 走看看