Type.GetMembers
Method
Definition
Overloads
| GetMembers() |
Returns all the public members of the current Type. |
| GetMembers(BindingFlags) |
When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints. |
GetMembers()
Returns all the public members of the current Type.
public System.Reflection.MemberInfo[] GetMembers ();
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.
Examples
The following code example demonstrates how to use the GetMembers() method overload to collect information about all public members of a specified class.
ref class MyClass
{
public:
int myInt;
String^ myString;
MyClass(){}
void Myfunction(){}
};
int main()
{
try
{
MyClass^ myObject = gcnew MyClass;
array<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( "\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 : {0}", e->Message );
}
}
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 );
}
}
}
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
Remarks
Members include properties, methods, fields, events, and so on.
The GetMembers method does not return members in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which members are returned, because that order varies.
This method overload calls the GetMembers(BindingFlags) method overload, with BindingFlags | BindingFlags | BindingFlags (BindingFlagsOrBindingFlagsOrBindingFlags in Visual Basic). It will not find class initializers (.cctor). To find class initializers, use an overload that takes BindingFlags, and specify BindingFlags | BindingFlags (BindingFlagsOrBindingFlags in Visual Basic). You can also get the class initializer using the TypeInitializer property.
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. |
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.
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.
Custom attributes are not part of the common type system.
If the current Type represents a constructed generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the members of the class constraint, or the members of Object if there is no class constraint.
GetMembers(BindingFlags)
When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints.
public abstract System.Reflection.MemberInfo[] GetMembers (System.Reflection.BindingFlags bindingAttr);
- bindingAttr
- BindingFlags
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
-or-
Zero (Default), to return an empty array.
An array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints.
-or-
An empty array of type MemberInfo, if no members are defined for the current Type, or if none of the defined members match the binding constraints.
Examples
The following code example demonstrates how to use the GetMembers(BindingFlags) method overload to collect information about all public instance members of a specified class.
ref class MyClass
{
public:
int * myInt;
String^ myString;
MyClass(){}
void Myfunction(){}
};
int main()
{
try
{
MyClass^ MyObject = gcnew MyClass;
array<MemberInfo^>^myMemberInfo;
// Get the type of the class 'MyClass'.
Type^ myType = MyObject->GetType();
// Get the public instance members of the class 'MyClass'.
myMemberInfo = myType->GetMembers( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
Console::WriteLine( "\nThe public instance members of class '{0}' are : \n", myType );
for ( int i = 0; i < myMemberInfo->Length; i++ )
{
// Display name and type of the member of 'MyClass'.
Console::WriteLine( "'{0}' is a {1}", myMemberInfo[ i ]->Name, myMemberInfo[ i ]->MemberType );
}
}
catch ( SecurityException^ e )
{
Console::WriteLine( "SecurityException : {0}", e->Message );
}
//Output:
//The public instance members of class 'MyClass' are :
//'Myfunction' is a Method
//'ToString' is a Method
//'Equals' is a Method
//'GetHashCode' is a Method
//'GetType' is a Method
//'.ctor' is a Constructor
//'myInt' is a Field
//'myString' is a Field
}
class MyClass
{
public int myInt = 0;
public string myString = null;
public MyClass()
{
}
public void Myfunction()
{
}
}
class Type_GetMembers_BindingFlags
{
public static void Main()
{
try
{
MyClass MyObject = new MyClass();
MemberInfo [] myMemberInfo;
// Get the type of the class 'MyClass'.
Type myType = MyObject.GetType();
// Get the public instance members of the class 'MyClass'.
myMemberInfo = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine( "\nThe public instance members of class '{0}' are : \n", myType);
for (int i =0 ; i < myMemberInfo.Length ; i++)
{
// Display name and type of the member of 'MyClass'.
Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
}
}
catch (SecurityException e)
{
Console.WriteLine("SecurityException : " + e.Message );
}
//Output:
//The public instance members of class 'MyClass' are :
//'Myfunction' is a Method
//'ToString' is a Method
//'Equals' is a Method
//'GetHashCode' is a Method
//'GetType' is a Method
//'.ctor' is a Constructor
//'myInt' is a Field
//'myString' is a Field
}
}
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_BindingFlags
Public Shared Sub Main()
Try
Dim MyObject As New [MyClass]()
Dim myMemberInfo() As MemberInfo
' Get the type of the class 'MyClass'.
Dim myType As Type = MyObject.GetType()
' Get the public instance members of the class 'MyClass'.
myMemberInfo = myType.GetMembers((BindingFlags.Public Or BindingFlags.Instance))
Console.WriteLine(ControlChars.Cr + "The public instance 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 member of 'MyClass'.
Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
Next i
Catch e As SecurityException
Console.WriteLine(("SecurityException : " + e.Message.ToString()))
End Try
'Output:
'The public instance members of class 'MyClass' are :
''Myfunction' is a Method
''ToString' is a Method
''Equals' is a Method
''GetHashCode' is a Method
''GetType' is a Method
''.ctor' is a Constructor
''myInt' is a Field
''myString' is a Field
End Sub 'Main
End Class 'Type_GetMembers_BindingFlags
Remarks
Members include properties, methods, fields, events, and so on.
The GetMembers method does not return members in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which members are returned, because that order varies.
The following BindingFlags filter flags can be used to define which members to include in the search:
You must specify either
BindingFlags.InstanceorBindingFlags.Staticin order to get a return.Specify
BindingFlags.Publicto include public members in the search.Specify
BindingFlags.NonPublicto include non-public members (that is, private, internal, and protected members) in the search. Only protected and internal members on base classes are returned; private members on base classes are not returned.Specify
BindingFlags.FlattenHierarchyto includepublicandprotectedstatic members up the hierarchy;privatestatic members in inherited classes are not included.
The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnlyto search only the members declared on the Type, not members that were simply inherited.
Calling this method with only the Public flag or only the NonPublic flag will return the specified members and does not require any other flags.
See BindingFlags for more information.
To get the class initializer (.cctor) using this method overload, you must specify BindingFlags | BindingFlags (BindingFlagsOrBindingFlags in Visual Basic). You can also get the class initializer using the TypeInitializer property.
If the current T:System.Type represents a constructed generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the members of the class constraint, or the members of Object if there is no class constraint.