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.

適用於

另請參閱