同事说用放射可以做到,可是又说不出所以然。我网上看到的例程都是事先知道OBJECT里面的属性了的

解决方案 »

  1.   


    class MyClass
    {
       public int myInt = 0;
       public string myString = null;   public MyClass()
       {
       }
       public void Myfunction()
       {
       }
    }class Type_GetMembers
    {
       public static void Main()
       {
          try
          {
             MyClass myObject = new MyClass();
             MemberInfo[] myMemberInfo;          // Get the type of 'MyClass'.
             Type myType = myObject.GetType(); 
            
             // Get the information related to all public member's of 'MyClass'. 
             myMemberInfo = myType.GetMembers();
        
             Console.WriteLine( "\nThe members of class '{0}' are :\n", myType); 
             for (int i =0 ; i < myMemberInfo.Length ; i++)
             {
                // Display name and type of the concerned member.
                Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
             }
          }
          catch(SecurityException e)
          {
             Console.WriteLine("Exception : " + e.Message ); 
          }
       }
    }