Type.FindInterfaces() Method in C# with Examples

06 May, 2019
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:
// C# program to demonstrate the// Type.FindInterfaces(TypeFilter,// Object) Methodusing 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;    }}
filtered list of interface : System.Collections.IEnumerable.

Example 2:
// C# program to demonstrate the// Type.FindInterfaces(TypeFilter,// Object) Methodusing 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);        }    }}
myFilter should not be null
Exception Thrown: System.ArgumentNullException

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.type.findinterfaces?view=netcore-3.0
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
Extension Method in C#
C# | List Class
C# | .NET Framework (Basic Architecture and Component Stack)
HashSet in C# with Examples
Lambda Expressions in C#
Switch Statement in C#
Partial Classes in C#
Hello World in C#