C# | Type.GetMember() Method - GeeksforGeeks

05 Dec, 2019
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
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) Methodusing 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 Studentpublic 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) Methodusing 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 Studentpublic 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

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) // Methodusing 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 Studentpublic 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) Methodusing 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 Studentpublic 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

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) Methodusing 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 Studentpublic 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) Methodusing 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 Studentpublic 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:
https://docs.microsoft.com/en-us/dotnet/api/system.type.getmember?view=netframework-4.8
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# Dictionary with examples
C# | Method Overriding
C# | Constructors
C# | Class and Object
Introduction to .NET Framework
Extension Method in C#
Difference between Ref and Out keywords in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Delegates
C# | Data Types