📜  C#中的Type.FindInterfaces()方法以及示例

📅  最后修改于: 2021-05-29 13:19:40             🧑  作者: Mango

Type.FindInterfaces(TypeFilter,Object)方法用于返回Type对象的数组,该数组表示由当前Type实现或继承的接口的过滤列表。在搜索过程中,将考虑由此类实现的所有接口,无论是由基类声明还是由此类本身声明。
此方法搜索基类层次结构,返回每个类实现的每个匹配接口以及每个接口实现的所有匹配接口(即,返回匹配接口的可传递闭包)。没有返回重复的接口。

句法:

参数:

  • filter :将接口与filterCriteria进行比较的委托。
  • filterCriteria :确定是否应该在返回的数组中包含接口的搜索条件。

返回值:该方法返回一个Type对象数组,该数组表示由当前Type实现或继承的接口的过滤列表;如果当前Type没有实现或继承与该过滤器匹配的接口,则返回一个Type类型的空数组。

异常:如果filter为null,则此方法将引发ArgumentNullException。

下面的程序说明了上述方法的用法:

范例1:

// 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;
    }
}
输出:
filtered list of interface : System.Collections.IEnumerable.

范例2:

// 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);
        }
    }
}
输出:
myFilter should not be null
Exception Thrown: System.ArgumentNullException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.findinterfaces?view=netcore-3.0