Type.Getmembers() Method In C#

The Type.GetMembers method is a potent reflection mechanism enabling programmers to retrieve details regarding a type's elements (such as fields, properties, methods, events, etc.) at runtime. Reflection in C# is a functionality that permits us to examine and engage with type, structure, and object information while the program is running.

The GetMembers method belongs to the System.Type class within the Common Type System (CTS) of the .NET Framework. It retrieves a set of MemberInfo instances, with each one denoting a specific member associated with the type.

Syntax:

It has the following syntax:

Example

public System.Reflection.MemberInfo[] GetMembers ();

This function generates an array of MemberInfo instances that encompass all public members associated with the current type. When the current Type possesses public members, it yields an empty array of type MemberInfo.

Example:

Let's consider a scenario to demonstrate the implementation of the Type.GetMember method in the C# programming language.

Filename: GetMembers.cs

Example

// Program to implement the GetMembers() function in c#
using System; 
using System.Globalization; 
using System.Reflection; 

// Defining Empty class 
public class Empty { } 

class GetMembers{ 

	// Main Method 
	public static void Main() 
	{ 
		// object declaration
		Type objTy = typeof(Empty); 

		// try-catch block for handling Exception 
		try { 

			// Getting an array of MemberInfos using the GetMembers() function.  
			MemberInfo[] information = objTy.GetMembers(); 

			// result statement
			Console.WriteLine("The Fields of the current type is as follows: "); 

			for (int i = 0; i < information.Length; i++) 
				Console.WriteLine(" {0}", information[i]); 
		} 

		// catch argument
		catch (ArgumentNullException ex) 
		{ 
			Console.Write("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
}

Output:

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's consider a scenario to demonstrate the implementation of the Type.GetMember method in the C# programming language.

Filename: GetMembers2.cs

Example

using System; 
using System.Globalization; 
using System.Reflection; 

class GetMembers{ 

	// Main Method 
	public static void Main() 
	{ 
		// object declaration
		Type objTy = typeof(int); 

		// try-catch block for handling Exception 
		try { 

			// Getting an array of MemberInfos using the GetMembers() function.  
			MemberInfo[] information = objTy.GetMembers(); 

			// result statement
			Console.WriteLine("The Fields of the current type is as Follows: "); 

			for (int i = 0; i < information.Length; i++) 
				Console.WriteLine(" {0}", information[i]); 
		} 

		// catch argument
		catch (ArgumentNullException ex) 
		{ 
			Console.Write("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
}

Output:

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 function scans for properties specified for the existing Type based on the binding restrictions provided upon being redefined in a subclass.

Syntax:

It has the following syntax:

Example

public abstract System.Reflection.MemberInfo[] GetMembers
(System.Reflection.BindingFlags bindingAttribute);

This function takes in a bitmask of BindingFlags to decide the search approach or returns an empty array if no criteria are provided.

Return Value:

This function returns an array of MemberInfo objects that represent all members declared within the current Type and meeting the specified binding restrictions. In cases where there are no members declared for the current Type or none of the existing members satisfy the binding conditions, an empty array containing MemberInfo type is returned.

The upcoming examples illustrate the implementation of the method mentioned earlier:

Example 1:

Example

using System; 
using System.Globalization; 
using System.Reflection; 

// an Empty class declaration
public class Empty { } 

class BindingFlag{ 

	// Main Method 
	public static void Main() 
	{ 
		//object initialization for Tyoe
		Type objType = typeof(Empty); 

		//try block for exception handling
		try { 

			// the array feilds retrival using GetMembers() method
			
                    MemberInfo[] information = objType.GetMembers(BindingFlags.Public|      
                      BindingFlags.Instance); 

			// the print statement
			Console.WriteLine("The Fields of the current type is as Follows: "); 
			for (int i = 0; i < information.Length; i++) 
				Console.WriteLine(" {0}", information[i]); 
		} 

		// the catch block of ArgumentNullException
		catch (ArgumentNullException ex) { 
			Console.WriteLine("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
}

Output:

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's consider another instance to demonstrate the implementation of the Type.GetMember technique in C#.

Example

using System; 
using System.Globalization; 
using System.Reflection; 


class BindingFlag{ 

	// Main Method 
	public static void Main() 
	{ 
		//object initialization for Tyoe
		Type objType = typeof(int); 

		//try block for exception handling
		try { 

			// the array fields retrieval using GetMembers() method
	 MemberInfo[] information = objType.GetMembers(BindingFlags.Public| 
             BindingFlags.Static); 
			// the print statement
			Console.WriteLine("The Field of the current type is as Follows: "); 
			for (int i = 0; i < information.Length; i++) 
				Console.WriteLine(" {0}", information[i]); 
		} 

		// the catch block of ArgumentNullException
		catch (ArgumentNullException ex) { 
			Console.WriteLine("The name is null."); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
}

Output:

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

Input Required

This code uses input(). Please provide values below: