Type.GetInterfaces 方法

定义

在派生类中重写时,获取当前 Type实现或继承的所有接口。

public:
 abstract cli::array <Type ^> ^ GetInterfaces();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)]
public abstract Type[] GetInterfaces();
public abstract Type[] GetInterfaces();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)>]
abstract member GetInterfaces : unit -> Type[]
abstract member GetInterfaces : unit -> Type[]
Public MustOverride Function GetInterfaces () As Type()

返回

Type[]

表示当前Type实现或继承的所有接口的对象数组Type

-或-

一个空数组,如果当前Type未实现或继承接口,则为空Type数组。

实现

属性

例外

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

示例

以下示例获取指定类的类型,并显示该类型实现或继承的所有接口。 若要编译Visual Basic示例,请使用以下编译器命令:

vbc type_getinterfaces1.vb /r:System.Web.dll /r:System.dll

using System;
using System.Collections.Generic;

public class Example
{
    static void Main()
    {
        Console.WriteLine("\r\nInterfaces implemented by Dictionary<int, string>:\r\n");

        foreach (Type tinterface in typeof(Dictionary<int, string>).GetInterfaces())
        {
            Console.WriteLine(tinterface.ToString());
        }

        //Console.ReadLine()      // Uncomment this line for Visual Studio.
    }
}

/* This example produces output similar to the following:

Interfaces implemented by Dictionary<int, string>:

System.Collections.Generic.IDictionary`2[System.Int32,System.String]
System.Collections.Generic.ICollection`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]]
System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]]
System.Collection.IEnumerable
System.Collection.IDictionary
System.Collection.ICollection
System.Runtime.Serialization.ISerializable
System.Runtime.Serialization.IDeserializationCallback
 */
open System.Collections.Generic

printfn "\nInterfaces implemented by Dictionary<int, string>:\n"

for tinterface in typeof<Dictionary<int, string>>.GetInterfaces() do
    printfn $"{tinterface}"

(* This example produces output similar to the following:

Interfaces implemented by Dictionary<int, string>:

System.Collections.Generic.IDictionary`2[System.Int32,System.String]
System.Collections.Generic.ICollection`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]]
System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]]
System.Collection.IEnumerable
System.Collection.IDictionary
System.Collection.ICollection
System.Runtime.Serialization.ISerializable
System.Runtime.Serialization.IDeserializationCallback
 *)
Imports System.Collections.Generic

Public Class Example

    Shared Sub Main()

        Console.WriteLine(vbCrLf & _
            "Interfaces implemented by Dictionary(Of Integer, String):" & vbCrLf)
        
        For Each tinterface As Type In GetType(Dictionary(Of Integer, String)).GetInterfaces()

            Console.WriteLine(tinterface.ToString())

        Next

        'Console.ReadLine()      ' Uncomment this line for Visual Studio. 
    End Sub
End Class

' This example produces output similar to the following:
'
'Interfaces implemented by Dictionary(Of Integer, String):
'System.Collections.Generic.IDictionary`2[System.Int32,System.String]
'System.Collections.Generic.ICollection`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]]
'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]]
'System.Collection.IEnumerable
'System.Collection.IDictionary
'System.Collection.ICollection
'System.Runtime.Serialization.ISerializable
'System.Runtime.Serialization.IDeserializationCallback

注解

在 .NET 6 及更早版本中,GetInterfaces 方法不会按特定顺序返回接口,如字母顺序或声明顺序。 你的代码不得依赖于返回接口的顺序,因为该顺序会有所不同。 但是,从 .NET 7 开始,排序是基于程序集中的元数据排序确定性的。

如果当前 Type 表示构造的泛型类型,此方法将返回 Type 具有由相应类型参数替换的类型参数的对象。

如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索接口约束以及从类或接口约束继承的任何接口。

适用于

另请参阅