Type.FindInterfaces() Method in C# with Examples
Last Updated :
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:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
try {
Type type = typeof (System.String);
TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
object filterCriteria = "System.Collections.IEnumerable" ;
Type[] myInterfaces = type.FindInterfaces(myFilter,
filterCriteria);
for ( int j = 0; j < myInterfaces.Length; j++)
Console.WriteLine( "filtered list of interface : {0}." ,
myInterfaces[j].ToString());
}
catch (ArgumentNullException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
public static bool MyInterfaceFilter(Type typeObj,
Object criteriaObj)
{
if (typeObj.ToString() == criteriaObj.ToString())
return true ;
else
return false ;
}
}
|
Output:
filtered list of interface : System.Collections.IEnumerable.
Example 2:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
try {
Type type = typeof (System.String);
TypeFilter myFilter = null ;
object filterCriteria = "System.Collections.IEnumerable" ;
Type[] myInterfaces = type.FindInterfaces(myFilter,
filterCriteria);
for ( int j = 0; j < myInterfaces.Length; j++)
Console.WriteLine( "filtered list of interface : {0}." ,
myInterfaces[j].ToString());
}
catch (ArgumentNullException e)
{
Console.WriteLine( "myFilter should not be null" );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
myFilter should not be null
Exception Thrown: System.ArgumentNullException
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...