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:

Example

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

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

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 us take an example to illustrate the Type.GetMember method in C#.

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 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:

Example

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

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:

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 us take another example to illustrate the Type.GetMember method 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: