C# | Type.GetMember() Method
Type.GetMember() Method is used to get the specified members of the current Type. There are 3 methods in the overload list of this method as follows:
- GetMember(String) Method
- GetMember(String, BindingFlags) Method
- GetMember(String, MemberTypes, BindingFlags) Method
GetMember(String) Method
This method searches for the public members with the specified name.
Syntax: public System.Reflection.MemberInfo[] GetMember (string name);
Here, it takes the string containing the name of the public members to get.Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name, if found otherwise, an empty array.
Exception: This method throws ArgumentNullException if name is null.
Below programs illustrate the use of the above-discussed method:
Example 1:
// C# program to demonstrate the // Type.GetMember(String) Method using System; using System.Globalization; using System.Reflection; class GFG { // Main Method public static void Main() { // Declaring and initializing object of Type Type objType = typeof (Student); // try-catch block for handling Exception try { MemberInfo[] info = objType.GetMember( "Name" ); // Display the Result Console.WriteLine( "Members of current type is as Follow: " ); for ( int i = 0; i < info.Length; i++) Console.WriteLine( " {0}" , info[i]); } // catch ArgumentNullException here catch (ArgumentNullException e) { Console.Write( "name is null." ); Console.Write( "Exception Thrown: " ); Console.Write( "{0}" , e.GetType(), e.Message); } } } // Defining class Student public class Student { public string Name = "Rahul" ; public string Dept = "Electrical" ; public int Roll = 10; public static int id = 02; } |
Members of current type is as Follow: System.String Name
Example 2: For ArgumentNullException
// C# program to demonstrate the // Type.GetMember(String) Method using System; using System.Globalization; using System.Reflection; class GFG { // Main Method public static void Main() { // Declaring and initializing object of Type Type objType = typeof (Student); // try-catch block for handling Exception try { MemberInfo[] info = objType.GetMember( null ); // Display the Result Console.WriteLine( "Members of current type is as Follow: " ); for ( int i = 0; i < info.Length; i++) Console.WriteLine( " {0}" , info[i]); } // catch ArgumentNullException here catch (ArgumentNullException e) { Console.WriteLine( "name is null." ); Console.Write( "Exception Thrown: " ); Console.Write( "{0}" , e.GetType(), e.Message); } } } // Defining class Student public class Student { public string Name = "Rahul" ; public string Dept = "Electrical" ; public int Roll = 10; public static int id = 02; } |
name is null. Exception Thrown: System.ArgumentNullException
GetMember(String, BindingFlags) Method
This method is used to search for the specified members, using the specified binding constraints.
Syntax: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.BindingFlags bindingAttr);
Parameters:
name: It is the string containing the name of the members to get.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted. or, Zero, to return an empty array.Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name if found otherwise, an empty array.
Exception: This method throws ArgumentNullException if name is null.
Below programs illustrate the use of the above-discussed method:
Example 1:
// C# program to demonstrate the // Type.GetMember(String, BindingFlags) // Method using System; using System.Globalization; using System.Reflection; class GFG { // Main Method public static void Main() { // Declaring and initializing object of Type Type objType = typeof (Student); // try-catch block for handling Exception try { MemberInfo[] info = objType.GetMember( "Name" , BindingFlags.Public | BindingFlags.Instance); // Display the Result Console.WriteLine( "Members of current type is as Follow: " ); for ( int i = 0; i < info.Length; i++) Console.WriteLine( " {0}" , info[i]); } // catch ArgumentNullException here catch (ArgumentNullException e) { Console.WriteLine( "name is null." ); Console.Write( "Exception Thrown: " ); Console.Write( "{0}" , e.GetType(), e.Message); } } } // Defining class Student public class Student { public string Name = "Rahul" ; public string Dept = "Electrical" ; public int Roll = 10; public static int id = 02; } |
Members of current type is as Follow: System.String Name
Example 2: For ArgumentNullException
// C# program to demonstrate the // Type.GetMember(String, BindingFlags) Method using System; using System.Globalization; using System.Reflection; class GFG { // Main Method public static void Main() { // Declaring and initializing object of Type Type objType = typeof (Student); // try-catch block for handling Exception try { MemberInfo[] info = objType.GetMember( null , BindingFlags.Public | BindingFlags.Instance); // Display the Result Console.WriteLine( "Members of current type is as Follow: " ); for ( int i = 0; i < info.Length; i++) Console.WriteLine( " {0}" , info[i]); } // catch ArgumentNullException here catch (ArgumentNullException e) { Console.WriteLine( "name is null." ); Console.Write( "Exception Thrown: " ); Console.Write( "{0}" , e.GetType(), e.Message); } } } // Defining class Student public class Student { public string Name = "Rahul" ; public string Dept = "Electrical" ; public int Roll = 10; public static int id = 02; } |
name is null. Exception Thrown: System.ArgumentNullException
GetMember(String, MemberTypes, BindingFlags) Method
This method is used to search for the specified members of the specified member type, using the specified binding constraints.
Syntax: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.MemberTypes type, System.Reflection.BindingFlags bindingAttr);
Parameters:
name: It is the string containing the name of the members to get.
type: It is the value to search for.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted or, Zero, to return an empty array.Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name if found otherwise, an empty array.
Exception: This method throw ArgumentNullException if the name is null.
Below programs illustrate the use of the above-discussed method:
Example 1:
// C# program to demonstrate the // Type.GetMember(String, MemberTypes, // BindingFlags) Method using System; using System.Globalization; using System.Reflection; class GFG { // Main Method public static void Main() { // Declaring and initializing object of Type Type objType = typeof (Student); // try-catch block for handling Exception try { MemberInfo[] info = objType.GetMember( "returnNull" , MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance); // Display the Result Console.WriteLine( "Members of current type is as Follow: " ); for ( int i = 0; i < info.Length; i++) Console.WriteLine( " {0}" , info[i]); } // catch ArgumentNullException here catch (ArgumentNullException e) { Console.WriteLine( "name is null." ); Console.Write( "Exception Thrown: " ); Console.Write( "{0}" , e.GetType(), e.Message); } } } // Defining class Student public class Student { public string Name = "Rahul" ; public string Dept = "Electrical" ; public int Roll = 10; public static int id = 02; public void returnNull() {} } |
Members of current type is as Follow: Void returnNull()
Example 2: For ArgumentNullException
// C# program to demonstrate the // Type.GetMember(String, MemberTypes, // BindingFlags) Method using System; using System.Globalization; using System.Reflection; class GFG { // Main Method public static void Main() { // Declaring and initializing object of Type Type objType = typeof (Student); // try-catch block for handling Exception try { MemberInfo[] info = objType.GetMember( null , MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance); // Display the Result Console.WriteLine( "Members of current type is as Follow: " ); for ( int i = 0; i < info.Length; i++) Console.WriteLine( " {0}" , info[i]); } // catch ArgumentNullException here catch (ArgumentNullException e) { Console.WriteLine( "name is null." ); Console.Write( "Exception Thrown: " ); Console.Write( "{0}" , e.GetType(), e.Message); } } } // Defining class Student public class Student { public string Name = "Rahul" ; public string Dept = "Electrical" ; public int Roll = 10; public static int id = 02; public void returnNull() {} } |
name is null. Exception Thrown: System.ArgumentNullException
Reference:
Please Login to comment...