Type.Findinterfaces() Method In C#

In this post, we'll explore the Type.FindInterfaces method in C# along with its syntax and illustrations.

What is the Type.FindInterfaces?

The Type.FindInterfaces method proves to be valuable in fetching a set of interfaces that a specific type implements. This method belongs to the System.Type class, offering functionalities for inspecting types during runtime. It enables developers to analyze classes, structs, enums, and more to gather details about the interfaces they implement.

This function analyzes the inheritance tree of the base class and provides a list of interfaces implemented by each class, as well as interfaces implemented by those interfaces. It ensures that no duplicate interfaces are included in the results.

Syntax:

It has the following syntax:

Example

public virtual Type[] FindInterfaces (System.Reflection.TypeFilter filters, object filtersCri);

Parameters:

  • Filters: The module that compares interfaces based on filter criteria.
  • filtersCriteria: It is thesearch criteria used to decide if an interface should be included within the returned array.

The output of the function is an array comprising Type instances. Subsequently, it performs a filtration process on this collection of interfaces and produces a new array that includes the corresponding Type objects.

This function will raise an ArgumentNullException if the filter parameter is null.

Example 1:

Let's consider an instance to apply the Type.FindInterfaces(TypeFilter, Object) method in C#:

Example

// Program to implement the 
// Type.FindInterfaces(TypeFilter, 
// Object) in C#
using System; 
using System.Globalization; 
using System.Reflection; 

class FindInterface{ 

	// Main Method 
	public static void Main() 
	{ 

		//A try-catch block for handling the exception
		try { 

			// object and its type declaration
			Type types = typeof(System.String); 

			//Declare and initialize a TypeFilter 
			//Datatype object to support the 
			//delegate in comparing interfaces 
			//against filter criteria. 
			TypeFilter myFilterValue = new TypeFilter(MyInterfaceFilter); 

			// Defining and initializing filter criteria:
			// Object. It searches for 
			//criteria that define whether 
			//an interface should be contained in the returned array. 
			object filterCriterias = "System.Collections.IEnumerable"; 

			// Obtaining a filtered list of 
			//interfaces with the FindInterfaces() function. 
		Type[]myInterfaceValue = types.FindInterfaces(myFilterValue,filterCriterias); 

			// print statement for interfaces
			for (int j = 0; j < myInterfaceValue.Length; j++) 
				Console.WriteLine("The filtered list of the interface are: {0}.", myInterfaceValue[j].ToString()); 
		} 

		// An argument to handle the exception
		catch (ArgumentNullException ex) 
		{ 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 

	// Defining MyInterfaceFilter to 
	//handle specific filtration situations.  
	public static bool MyInterfaceFilter(Type typeObjs, Object criteriaObjs) 
	{ 
		if (typeObjs.ToString() == criteriaObjs.ToString()) 
			return true; 
		else
			return false; 
	} 
}

Output:

Output

The filtered list of the interface are: System.Collections.IEnumerable.

Example 2:

Let's consider another scenario to apply the Type.FindInterfaces(TypeFilter, Object) method in C#:

Example

using System; 
using System.Globalization; 
using System.Reflection; 

class FindInterface{ 

	// Main Method 
	public static void Main() 
	{ 

		//A try-catch block for handling the exception
		try { 

			// object and its type declaration
			Type types = typeof(System.String); 

			//Declare and initialize a TypeFilter 
			//Datatype object to support 
			///the delegate in comparing interfaces   
			//against filter criteria. 
			TypeFilter myFilterValue = null;

			// Defining and initializing filter criteria:
			// Object. It searches for criteria 
			//that defines whether 
			//an interface should be contained in the returned array. 
			object filterCriterias = "System.Collections.IEnumerable"; 

			// Obtaining a filtered list of interfaces 
			//with the FindInterfaces() function. 
			Type[] myInterfaceValue = types.FindInterfaces(myFilterValue, 
											filterCriterias); 

			// print statement for interfaces
			for (int j = 0; j < myInterfaceValue.Length; j++) 
				Console.WriteLine("The filtered list of the interface are: {0}.",    
                        myInterfaceValue[j].ToString()); 
		} 

		// An argument to handle the exception
		catch (ArgumentNullException ex) 
		{ 
		    Console.WriteLine("myFilterValue should not be null"); 
			Console.Write("The Exception is Thrown: "); 
			Console.Write("{0}", ex.GetType(), ex.Message); 
		} 
	} 
}

Output:

Output

myFilterValue should not be null
The Exception is Thrown: System.ArgumentNullException

Input Required

This code uses input(). Please provide values below: