C# | Type.GetMembers() Method - GeeksforGeeks

10 Dec, 2019
Type.GetMembers() Method is used to get the members (properties, methods, fields, events, and so on) of the current Type. There are 2 methods in the overload list of this method as follows:
GetMembers() Method
GetMembers(BindingFlags) Method
This method is used to return all the public members of the current Type.
Syntax: public System.Reflection.MemberInfo[] GetMembers ();
Return Value: This method returns an array of MemberInfo objects representing all the public members of the current Type Or an empty array of type MemberInfo if the current Type does not have public members.
Below programs illustrate the use of Type.GetMembers() Method:
Example 1:
// C# program to demonstrate the// Type.GetMember() Methodusing System;using System.Globalization;using System.Reflection;  // Defining Empty classpublic class Empty { }  class GFG {      // Main Method    public static void Main()    {        // Declaring and initializing object of Type        Type objType = typeof(Empty);          // try-catch block for handling Exception        try {              // Getting array of MemberInfos by            // using GetMembers() Method            MemberInfo[] info = objType.GetMembers();              // Display the Result            Console.WriteLine("Fields 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);        }    }}
Fields of current type is as Follow: 
 Boolean Equals(System.Object)
 Int32 GetHashCode()
 System.Type GetType()
 System.String ToString()
 Void .ctor()

Example 2:
// C# program to demonstrate the// Type.GetMember() 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(int);          // try-catch block for handling Exception        try {              // Getting array of MemberInfos by            // using GetMembers() Method            MemberInfo[] info = objType.GetMembers();              // Display the Result            Console.WriteLine("Fields of current type is as Follow: ");            for (int i = 0; i < 6; 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);        }    }}
Fields of current type is as Follow: 
 Int32 CompareTo(System.Object)
 Int32 CompareTo(Int32)
 Boolean Equals(System.Object)
 Boolean Equals(Int32)
 Int32 GetHashCode()
 System.String ToString()

This method is used to search for the members defined for the current Type, using the specified binding constraints when overridden in a derived class.
Syntax: public abstract System.Reflection.MemberInfo[] GetMembers (System.Reflection.BindingFlags bindingAttr);Here, it takes a bitmask comprised of one or more BindingFlags that specify how the search is conducted or,Zero (Default), to return an empty array.
Return Value: This method returns an array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints Or an empty array of type MemberInfo, if no members are defined for the current Type, or if none of the defined members match the binding constraints.
Below programs illustrate the use of the above-discussed method:
Example 1:
// C# program to demonstrate the// Type.GetMembers(BindingFlags)// Methodusing System;using System.Globalization;using System.Reflection;  // Defining class Emptypublic class Empty { }  class GFG {      // Main Method    public static void Main()    {        // Declaring and initializing object of Type        Type objType = typeof(Empty);          // try-catch block for handling Exception        try {              // Getting array of Fields by            // using GetField() Method            MemberInfo[] info = objType.GetMembers(BindingFlags.Public                                                   | BindingFlags.Instance);              // Display the Result            Console.WriteLine("Fields 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);        }    }}
Fields of current type is as Follow: 
 Boolean Equals(System.Object)
 Int32 GetHashCode()
 System.Type GetType()
 System.String ToString()
 Void .ctor()

Example 2:
// C# program to demonstrate the// Type.GetMembers(BindingFlags)// Methodusing System;using System.Globalization;using System.Reflection;  // Defining class Emptypublic class Empty { }  class GFG {      // Main Method    public static void Main()    {        // Declaring and initializing object of Type        Type objType = typeof(int);          // try-catch block for handling Exception        try {              // Getting array of Fields by            // using GetField() Method            MemberInfo[] info = objType.GetMembers(BindingFlags.Public                                                   | BindingFlags.Static);              // Display the Result            Console.WriteLine("Fields 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);        }    }}
Fields of current type is as Follow: 
 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)
 Boolean TryParse(System.String, Int32 ByRef)
 Boolean TryParse(System.String, System.Globalization.NumberStyles, System.IFormatProvider, Int32 ByRef)
 System.Int32 MaxValue
 System.Int32 MinValue

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.type.getmembers?view=netframework-4.8
shubham_singh
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Partial Classes in C#
C# | Inheritance
C# | List Class
Difference between Hashtable and Dictionary in C#
Convert String to Character Array in C#