Type.FindMembers() Method in C# with Examples

Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Method is used to return a filtered array of MemberInfo objects of the specified member type.

Syntax:

public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter filter, object filterCriteria);

Parameters:

  • memberType: It indicates that what type of member should be searched.
  • bindingAttr: It used to specify how the search is conducted or Zero, to return null.
  • filter: It does the comparisons, returning true if the member currently being inspected matches the filterCriteria and false otherwise.
  • filterCriteria: The search criteria that determines whether a member is returned in the array of MemberInfo objects.

The following BindingFlags filter flags can be used to define which members to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
  • Specify BindingFlags.Instance to include instance members in the search.
  • Specify BindingFlags.Static to include static members in the search.
  • Specify BindingFlags.Public to include public members in the search.
  • Specify BindingFlags.NonPublic to include non-public members (that is, private, internal, and protected members) in the search.

Return Value: This method returns A filtered array of MemberInfo objects of the specified member type. or An empty array of type MemberInfo,

Exception: This method throws ArgumentNullException if filter is null.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.FindMembers(MemberTypes, 
// BindingFlags, MemberFilter, 
// Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(string);
  
        // Creating try-catch block for handling Exception
        try {
  
            // Declaring and initializing the object of MemberTypes
            // that indicates the type of member to search for.
            MemberTypes mt = MemberTypes.All;
  
            // Declaring and initializing the object of BindingFlags
            // that specify how the search is conducted.
            BindingFlags ba = BindingFlags.Public
                          | BindingFlags.Instance;
  
            // Declaring and intializing MemberFilter
            // which help the delegate that compares
            // the MemberInfo against filterCriteria.
            MemberFilter mf = new MemberFilter(Search);
  
            // Declaring and intializing object of filterCriteria
            // the search criteria that determines whether a member is
            // returned in the array of MemberInfo objects or not
            object filterCriteria = "Equals";
  
            // Getting filterd array of MemberInfo by
            // using FindMembers() Method
            MemberInfo[] info = objType.FindMembers(mt, ba, mf, filterCriteria);
  
            // Display the Result
            for (int index = 0; index < info.Length; index++)
                Console.WriteLine("Result of FindMembers -  {0}",
                                        info[index].ToString());
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining MyInterfaceFilter
    // which helps to fix the certain condition
    // on which filtration took place
    public static bool Search(MemberInfo info, Object obj)
    {
  
        // Compare the name of the member
        // function with the filter criteria.
        if (info.Name.ToString() == obj.ToString())
            return true;
        else
            return false;
    }
}

chevron_right


Output:

Result of FindMembers -  Boolean Equals(System.Object)
Result of FindMembers -  Boolean Equals(System.String)
Result of FindMembers -  Boolean Equals(System.String, System.StringComparison)

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.