Export (0) Print
Expand All

Type.GetMembers Method ()

.NET Framework 1.1

Returns all the public members of the current Type.

[Visual Basic]
Overloads Public Function GetMembers() As MemberInfo()
[C#]
public MemberInfo[] GetMembers();
[C++]
public: MemberInfo* GetMembers() [];
[JScript]
public function GetMembers() : MemberInfo[];

Return Value

An array of MemberInfo objects representing all the public members of the current Type.

-or-

An empty array of type MemberInfo, if the current Type does not have public members.

Remarks

Members include properties, methods, fields, events, and so on.

Class initializers are available through GetMember, GetMembers, FindMembers, and GetConstructors.

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type Static Non-Static
Constructor No No
Field No Yes. A field is always hide-by-name-and-signature.
Event Not applicable The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.
Method No Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
Nested Type No No
Property Not applicable The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.
  1. Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
  2. For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
  3. Custom attributes are not part of the common type system.

Example

[Visual Basic] 
Class [MyClass]
   Public myInt As Integer = 0
   Public myString As String = Nothing
   
   
   Public Sub New()
   End Sub 'New
   
   Public Sub Myfunction()
   End Sub 'Myfunction
End Class '[MyClass]

Class Type_GetMembers
   
   Public Shared Sub Main()
      Try
         Dim myObject As New [MyClass]()
         Dim myMemberInfo() As MemberInfo
         
         ' Get the type of 'MyClass'.
         Dim myType As Type = myObject.GetType()
         
         ' Get the information related to all public member's of 'MyClass'. 
         myMemberInfo = myType.GetMembers()
         
         Console.WriteLine(ControlChars.Cr + "The members of class '{0}' are :" + ControlChars.Cr, myType)
         Dim i As Integer
         For i = 0 To myMemberInfo.Length - 1
            ' Display name and type of the concerned member.
            Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
         Next i

      Catch e As SecurityException
         Console.WriteLine(("Exception : " + e.Message.ToString()))
      End Try
   End Sub 'Main
End Class 'Type_GetMembers

[C#] 
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 ); 
      }
   }
}

[C++] 
__gc class MyClass {
public:
    int myInt;
    String* myString;

    MyClass() {
    }
    void Myfunction() {
    }
};

int main() {
    try {
        MyClass* myObject = new MyClass();
        MemberInfo* myMemberInfo[];

        // Get the type of 'MyClass'.
        Type*  myType = myObject->GetType();

        // Get the information related to all public members of 'MyClass'.
        myMemberInfo = myType->GetMembers();

        Console::WriteLine(S"\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(S"'{0}' is a {1}", myMemberInfo[i]->Name,__box( myMemberInfo[i]->MemberType));
        }
    } catch (SecurityException* e) {
        Console::WriteLine(S"Exception : {0}", e->Message);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | Type.GetMembers Overload List | MemberInfo | GetMember | GetDefaultMembers | FindMembers

Show:
© 2014 Microsoft