new一个现有class,如何得知class中有几个属性?我想取此class所有的属性表示名

解决方案 »

  1.   

      using   System;  
      using   System.Reflection;  
       
      class   TestField  
      {  
          private   int   i=12;  
          protected   string   sabc   =   "a";  
          public   DateTime   dt   =   DateTime.Now;  
       
          static   void   Main()  
          {  
      TestField   tf   =   new   TestField();  
                        foreach   (FieldInfo   fi   in   tf.GetType().GetFields(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic))  
      {  
      object   o   =   fi.GetValue(tf);  
      Console.WriteLine("name:{0}==>value:{1}",   fi.Name,   o.ToString());  
      }  
          }  
      }思归的做法,强
      

  2.   

      //同意楼上啊!用反射
          Type ty = typeof(Form);
          PropertyInfo[] py = ty.GetProperties();
          foreach (PropertyInfo aa in ty)
          {
           aa.Name;//这就是属性名了;
          }
          //要导入命名空间 System.Reflection;
      

  3.   

    typeof()括号里是要检索的类名,form 只是举个例子