Type.GetMembers Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the members (properties, methods, fields, events, and so on) of the current Type.
Overloads
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. |
GetMembers(BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints.
public:
abstract cli::array <System::Reflection::MemberInfo ^> ^ GetMembers(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.MemberInfo[] GetMembers (System.Reflection.BindingFlags bindingAttr);
abstract member GetMembers : System.Reflection.BindingFlags -> System.Reflection.MemberInfo[]
Public MustOverride Function GetMembers (bindingAttr As BindingFlags) As MemberInfo()
Parameters
- bindingAttr
- BindingFlags
A bitwise combination of the enumeration values that specify how the search is conducted.
-or-
Default to return an empty array.
Returns
An array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints.
-or-
An empty array if no members are defined for the current Type, or if none of the defined members match the binding constraints.
Implements
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
}
}
open System.Reflection
open System.Security
type MyClass =
val public myInt: int
val public myString: string
new () = { myInt = 0; myString = null}
member _.MyMethod() = ()
try
let MyObject = MyClass()
// Get the type of the class 'MyClass'.
let myType = MyObject.GetType()
// Get the public instance members of the class 'MyClass'.
let myMemberInfo = myType.GetMembers(BindingFlags.Public ||| BindingFlags.Instance)
printfn $"\nThe public instance members of class '{myType}' are : \n"
for i = 0 to myMemberInfo.Length - 1 do
// Display name and type of the member of 'MyClass'.
printfn $"'{myMemberInfo[i].Name}' is a {myMemberInfo[i].MemberType}"
with :? SecurityException as e ->
printfn $"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
Public Sub Myfunction()
End Sub
End Class
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
End Class
Remarks
Members include properties, methods, constructors, fields, events, and nested types.
For the GetMethods(BindingFlags)
overload to successfully retrieve method information, the bindingAttr
argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public. The sole exception is a method call with BindingFlags.NonPublic, which returns member information about nested types.
The following BindingFlags filter flags can be used to define which members to include in the search:
Specify
BindingFlags.Instance
to include instance methods.Specify
BindingFlags.Static
to include static methods.Specify
BindingFlags.Public
to include public methods in the search.Specify
BindingFlags.NonPublic
to include non-public methods (that is, private, internal, and protected methods) in the search. Only protected and internal methods on base classes are returned; private methods on base classes are not returned.Specify
BindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.Specify
BindingFlags.Default
alone to return an empty MethodInfo array.
The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnly
to search only the members declared on the Type, not members that were simply inherited.
See System.Reflection.BindingFlags for more information.
In .NET 6 and earlier versions, 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. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.
To get the class initializer (static constructor) using this method overload, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic in Visual Basic). You can also get the class initializer using the TypeInitializer property.
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.
See also
- MemberInfo
- BindingFlags
- DefaultBinder
- GetMember(String)
- GetDefaultMembers()
- FindMembers(MemberTypes, BindingFlags, MemberFilter, Object)
Applies to
GetMembers()
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Returns all the public members of the current Type.
public:
cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public:
virtual cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public System.Reflection.MemberInfo[] GetMembers ();
member this.GetMembers : unit -> System.Reflection.MemberInfo[]
abstract member GetMembers : unit -> System.Reflection.MemberInfo[]
override this.GetMembers : unit -> System.Reflection.MemberInfo[]
Public Function GetMembers () As MemberInfo()
Returns
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.
Implements
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 );
}
}
}
type MyClass =
val public myInt: int
val public myString: string
new () = { myInt = 0; myString = null}
member _.MyMethod() = ()
try
let myObject = MyClass()
// Get the type of 'MyClass'.
let myType = myObject.GetType()
// Get the information related to all public member's of 'MyClass'.
let myMemberInfo = myType.GetMembers()
printfn $"\nThe members of class '{myType}' are :\n"
for i = 0 to myMemberInfo.Length - 1 do
// Display name and type of the concerned member.
printfn $"'{myMemberInfo[i].Name}' is a {myMemberInfo[i].MemberType}"
with e ->
printfn $"Exception : {e.Message}"
Class [MyClass]
Public myInt As Integer = 0
Public myString As String = Nothing
Public Sub New()
End Sub
Public Sub Myfunction()
End Sub
End Class
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
End Class
Remarks
Members include properties, methods, constructors, fields, events, and nested types.
In .NET 6 and earlier versions, 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. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.
This method overload calls the GetMembers(BindingFlags) method overload, with BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static (BindingFlags.PublicOr
BindingFlags.InstanceOr
BindingFlags.Static in Visual Basic). It will not find class initializers (static constructors). To find class initializers, call the GetMembers(BindingFlags) overload, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic 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.
See also
- MemberInfo
- GetMember(String)
- GetDefaultMembers()
- FindMembers(MemberTypes, BindingFlags, MemberFilter, Object)