Type.FindInterfaces() Method in C# with Examples

Type.FindInterfaces(TypeFilter, Object) Method is used to return an array of Type objects which represents a filtered list of interfaces implemented or inherited by the current Type. All of the interfaces implemented by this class are considered during the search, whether declared by a base class or this class itself.
This method searches the base class hierarchy, returning each of the matching interfaces each class implements as well as all the matching interfaces each of those interfaces implements (that is, the transitive closure of the matching interfaces is returned). No duplicate interfaces are returned.

Syntax:

public virtual Type[] FindInterfaces (System.Reflection.TypeFilter filter, object filterCriteria);

Parameters:

  • filter: The delegate which compares the interfaces against filterCriteria.
  • filterCriteria: The search criteria which determines whether an interface should be included in the returned array.

Return Value: This method returns an array of Type objects representing a filtered list of the interfaces implemented or inherited by the current Type, or an empty array of type Type if no interfaces matching the filter are implemented or inherited by the current Type.

Exception: This method throws ArgumentNullException if filter is null.

Below programs illustrate the use of the above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.FindInterfaces(TypeFilter,
// Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Creating try-catch block 
        // to handle exceptions
        try {
  
            // Declaring and initializing 
            // object Type Datatype
            Type type = typeof(System.String);
  
            // Declaring and initializing the object 
            // of TypeFilter Datatype which help the
            // delegate that compares the interfaces 
            // against filterCriteria
            TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
  
            // Declaring and initializing filterCriteria 
            // object. It is used to search the criteria 
            // that determines whether an interface should
            // be included in the returned array.
            object filterCriteria = "System.Collections.IEnumerable";
  
            // Getting the filtered list of interface
            // using FindInterfaces() method
            Type[] myInterfaces = type.FindInterfaces(myFilter,
                                               filterCriteria);
  
            // Display the interfaces
            for (int j = 0; j < myInterfaces.Length; j++)
                Console.WriteLine("filtered list of interface : {0}.",
                                          myInterfaces[j].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 MyInterfaceFilter(Type typeObj,
                                   Object criteriaObj)
    {
        if (typeObj.ToString() == criteriaObj.ToString())
            return true;
        else
            return false;
    }
}

chevron_right


Output:

filtered list of interface : System.Collections.IEnumerable.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.FindInterfaces(TypeFilter,
// Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating try-catch block 
        // to handle exceptions
        try {
  
            // Declaring and initializing 
            // object Type Datatype
            Type type = typeof(System.String);
  
            // Declaring and initializing object 
            // of TypeFilter Datatype
            // which help the delegate that compares
            // the interfaces against filterCriteria.
            TypeFilter myFilter = null;
  
            // Declaring and initializing filterCriteria 
            // object. It is used to search the criteria 
            // that determines whether an interface should
            // be included in the returned array.
            object filterCriteria = "System.Collections.IEnumerable";
  
            // Getting the filtered list of interface
            // using FindInterfaces() method
            Type[] myInterfaces = type.FindInterfaces(myFilter,
                                               filterCriteria);
  
            // Display the interfaces
            for (int j = 0; j < myInterfaces.Length; j++)
                Console.WriteLine("filtered list of interface : {0}.",
                                          myInterfaces[j].ToString());
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
       {
            Console.WriteLine("myFilter should not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

myFilter should not be null
Exception Thrown: System.ArgumentNullException

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.