Type.FindInterfaces(TypeFilter, Object) 方法

定义

返回表示接口(由当前 Type 所实现或继承)的筛选列表的 Type 对象数组。

public virtual Type[] FindInterfaces(System.Reflection.TypeFilter filter, object? filterCriteria);
public virtual Type[] FindInterfaces(System.Reflection.TypeFilter filter, object filterCriteria);

参数

filter
TypeFilter

对照 filterCriteria 比较接口的委托。

filterCriteria
Object

确定接口是否应包括在返回数组中的搜索判据。

返回

Type[]

一个 Type 对象的数组,它表示由当前 Type 实现或继承的接口的已筛选的列表;如果当前 Type 未实现或继承与筛选器相匹配的接口,则为空数组。

实现

例外

filter 上声明的默认值为 null

调用静态初始值设定项时引发了异常。

示例

以下示例查找由指定类型实现或继承的指定接口,然后显示接口名称。

using System;
using System.Xml;
using System.Reflection;

public class MyFindInterfacesSample
{
    public static void Main()
    {
        try
        {
            XmlDocument myXMLDoc = new XmlDocument();
            myXMLDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" + "</book>");
            Type myType = myXMLDoc.GetType();

            // Specify the TypeFilter delegate that compares the
            // interfaces against filter criteria.
            TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
            String[] myInterfaceList = new String[2]
                {"System.Collections.IEnumerable",
                "System.Collections.ICollection"};
            for(int index=0; index < myInterfaceList.Length; index++)
            {
                Type[] myInterfaces = myType.FindInterfaces(myFilter,
                    myInterfaceList[index]);
                if (myInterfaces.Length > 0)
                {
                    Console.WriteLine("\n{0} implements the interface {1}.",
                        myType, myInterfaceList[index]);	
                    for(int j =0;j < myInterfaces.Length;j++)
                        Console.WriteLine("Interfaces supported: {0}.",
                            myInterfaces[j].ToString());
                }
                else
                    Console.WriteLine(
                        "\n{0} does not implement the interface {1}.",
                        myType,myInterfaceList[index]);	
            }
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(TargetInvocationException e)
        {
            Console.WriteLine("TargetInvocationException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }

    public static bool MyInterfaceFilter(Type typeObj,Object criteriaObj)
    {
        if(typeObj.ToString() == criteriaObj.ToString())
            return true;
        else
            return false;
    }
}

注解

派生类可以重写此方法。

Module.FilterTypeName还可以使用 类提供的 System.Reflection.ModuleModule.FilterTypeNameIgnoreCase 委托来代替System.Reflection.TypeFilter委托。

此类实现的所有接口都在搜索过程中考虑,无论是由基类声明还是由此类本身声明。

此方法搜索基类层次结构,返回每个类实现的每个匹配接口以及每个接口实现的所有匹配接口 (即,) 返回匹配接口的传递闭包。 不会返回重复的接口。

如果当前 Type 表示泛型类型或泛型方法定义中的类型参数, FindInterfaces 则搜索在类型参数的约束中声明的所有接口,以及通过约束中声明的接口继承的所有接口。 如果当前 Type 表示泛型类型的类型参数, FindInterfaces 则搜索该类型实现的所有接口,无论它们是否与约束匹配。

备注

FindInterfaces 可以返回泛型接口,即使在非泛型类型上也是如此。 例如,非泛型类型可能在 Visual Basic) IEnumerable(Of Integer) 中实现IEnumerable<int> (。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅