Javatpoint Logo
Javatpoint Logo

Type.GetMembers() Method in C#

The Type.GetMembers() function is a powerful reflection technique that allows developers to access information about a type's components (fields, properties, methods, events, and so on) during runtime. Reflection is a C# feature that allows us to observe and interact with type, assembling, and object metadata while they're executing.

The GetMembers() function is part of the System.Type class, and represents a type in the.NET Framework's Common Type System (CTS). It returns a collection of MemberInfo objects, each representing a member of the type.

Syntax:

It has the following syntax:

This function produces an array of MemberInfo objects containing all public members that belong to the current type. If the current Type has public members, it returns an empty array of type MemberInfo.

Example:

Let us take an example to illustrate the Type.GetMember() method in C#.

Filename: GetMembers.cs

Output:

The Fields of the current type is as follows: 
Boolean Equals(System.Object)
 Int32 GetHashCode()
 System.Type GetType()
 System.String ToString()
 Void .ctor()

Example 2:

Let us take an example to illustrate the Type.GetMember() method in C#.

Filename: GetMembers2.cs

Output:

The Fields of the current type is as follows: 
Int32 CompareTo(System.Object)
Int32 CompareTo(Int32)
 Boolean Equals(System.Object)
 Boolean Equals(Int32)
 Int32 GetHashCode()
 System.String ToString()

The GetMembers(BindingFlags) Method:

This method searches for members declared for the current Type using the binding constraints given when overridden in a derived class.

Syntax:

It has the following syntax:

This function accepts a bitmask of BindingFlags to determine the search method or returns an empty array if nothing is specified.

Return Value:

This function provides an array of MemberInfo objects indicating all members declared for the current Type that match the binding restrictions. If no members are declared for the current Type or none of the currently defined members meet the binding conditions, an empty array with the type MemberInfo is returned.

The following programs explain the application of the above method:

Example 1:

Output:

The Fields of the current type is as follows: 
Boolean Equals(System.Object)
 Int32 GetHashCode()
 System.Type GetType()
 System.String ToString()
 Void .ctor()

Example 2:

Let us take another example to illustrate the Type.GetMember() method in C#.

Output:

The Field of the current type is as follows: 
Int32 Parse(System.String)
Int32 Parse(System.String, System.Globalization.NumberStyles)
 Int32 Parse(System.String, System.IFormatProvider)
 Int32 Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider)
 Int32 Parse(System.ReadOnlySpan`1[System.Char], System.Globalization.NumberStyles, System.IFormatProvider)
Boolean TryParse(System.String, Int32 ByRef)
 Boolean TryParse(System.ReadOnlySpan`1[System.Char], Int32 ByRef)
 Boolean TryParse(System.String, System.Globalization.NumberStyles, System.IFormatProvider, Int32 ByRef)
 Boolean TryParse(System.ReadOnlySpan`1[System.Char], System.Globalization.NumberStyles, System.IFormatProvider, Int32 ByRef)
 System.Int32 MaxValue
System.Int32 MinValue






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news