Type.GetInterfaces Type.GetInterfaces Type.GetInterfaces Type.GetInterfaces Method

定义

当在派生类中重写时,获取由当前 Type 实现或继承的所有接口。When overridden in a derived class, gets all the interfaces implemented or inherited by the current Type.

public:
 abstract cli::array <Type ^> ^ GetInterfaces();
public abstract Type[] GetInterfaces ();
abstract member GetInterfaces : unit -> Type[]
Public MustOverride Function GetInterfaces () As Type()

返回

Type[]

表示由当前 Type 实现或继承的所有接口的 Type对象数组。An array of Type objects representing all the interfaces implemented or inherited by the current Type.

- 或 --or- 如果没有由当前 Type 实现或继承的接口,则为 Type 类型的空数组。An empty array of type Type, if no interfaces are implemented or inherited by the current Type.

实现

异常

调用静态初始值设定项时引发了异常。A static initializer is invoked and throws an exception.

示例

下面的示例获取指定类的类型并显示该类型实现或继承的所有接口。The following example gets the type of the specified class and displays all the interfaces that the type implements or inherits. 若要编译 Visual Basic 示例, 请使用以下编译器命令:To compile the Visual Basic example, use the following compiler commands:

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

using namespace System;
using namespace System::Collections::Generic;

void main()
{
    Console::WriteLine("\r\nInterfaces implemented by Dictionary<int, String^>:\r\n");
     
    for each (Type^ tinterface in Dictionary<int, String^>::typeid->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
 */
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
 */
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

注解

GetInterfaces方法不按特定顺序 (如字母顺序或声明顺序) 返回接口。The GetInterfaces method does not return interfaces in a particular order, such as alphabetical or declaration order. 你的代码不能依赖于接口的返回顺序, 因为该顺序会有所不同。Your code must not depend on the order in which interfaces are returned, because that order varies.

如果当前Type表示构造泛型类型, 则此方法Type返回类型参数替换为相应类型参数的对象。If the current Type represents a constructed generic type, this method returns the Type objects with the type parameters replaced by the appropriate type arguments.

如果当前Type表示泛型类型或泛型方法的定义中的类型参数, 则此方法会搜索接口约束以及从类或接口约束继承的任何接口。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

适用于

另请参阅