Type.Findinterfaces() Method In C#

In this article, we will discuss the Type.FindInterfaces in C# with its syntax and examples.

What is the Type.FindInterfaces?

The Type.FindInterfaces function is a useful tool for retrieving a collection of interfaces implemented by a certain type. The Type.FindInterfaces function is part of the System.Type class, which provides reflection features for examining types in runtime. It allows programmers to inspect classes, structures, enumerations, and other items to obtain information regarding the interfaces.

This function examines the base class hierarchy and returns each matching interface that each class executes, along with all matching interfaces that each of the corresponding interfaces implements. No duplicated interfaces are returned.

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.

Return Value: The function returns an array of Type objects. It then filters this list of interfaces and returns an array containing the matching Type objects.

This function throws ArgumentNullException in the event that the filter is null.

Example 1:

Let us take an example to implement the Type.FindInterfaces(TypeFilter, Object) 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 us take another example to implement the Type.FindInterfaces(TypeFilter, Object) 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: