就是说 我自己写了一个实体类,我现在要在不知道这个实体类对象具体有多少的属性时,将所有的遍历一边。最终可以知道这个对象的所有属性及值呢?
各位大虾:有想法的就指点指点小妹,小妹感激不尽!

解决方案 »

  1.   

    eg:
    PropertyInfo[] pis = typeof(类名).GetProperties();foreach (PropertyInfo pi in pis){var value1 = pi.GetValue(对象名, null);//得到属性的值var attr=pi.Name.ToString()//得到属性的名称}
      

  2.   

    /// <summary>
    /// 是否所有的属性不为null
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public static bool Finished(object t)
    {
    string tStr = string.Empty;
    System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    foreach (System.Reflection.PropertyInfo item in properties)
    {
    string name = item.Name;
    object value = item.GetValue(t, null);
    if (value == null)
    {
    return false;
    }
    }
    return true;
    }
      

  3.   

    PropertyInfo[] peroperties = typeof(A).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);  foreach (PropertyInfo property in peroperties)
      {property .Name;
       property .PropertyType;                 
    }
      

  4.   

    public static IList <T> FillList <T>(System.Data.IDataReader reader)
            {
                IList <T> lst= new List <T>();
                while (reader.Read())
                {
                      T RowInstance = Activator.CreateInstance <T>();
                    foreach (PropertyInfo Property in typeof(T).GetProperties())
                    {
                        foreach (BindingFieldAttribute FieldAttr in    Property.GetCustomAttributes(typeof(BindingFieldAttribute), true))
                      {
                      try
                        {
      int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
                              if (reader.GetValue(Ordinal) != DBNull.Value)
                              {
                                    Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
                                }
                            }
                            catch
                            {
                                break;
                            }
                        }
                    }
                    lst.Add(RowInstance);
                }
                return lst;
            }
      

  5.   

     foreach (PropertyInfo property in peroperties)遍历你的实体层
      {property .Name;
      property .PropertyType;   
    }PropertyInfo 实体层也就是你自己的实体
     property  关键字
    peroperties 定义的
    property .Name; 得到实体的名字
    property .PropertyType;   实体的属性
    然后你输出就知道了,要不在调试的时候看看你得到的每个属性的值也行