Type.GetInterfaces 方法

定義

在衍生類別中覆寫時,取得由目前 Type 所實作或繼承的所有介面。

C#
public abstract Type[] GetInterfaces ();

傳回

Type[]

Type 物件的陣列,代表目前 Type所實作或繼承的所有介面。

-或- Type 類型的空陣列 (如果目前 Type 未實作或繼承介面)。

實作

例外狀況

靜態初始設定式已叫用並擲回了例外狀況。

範例

下列範例會取得指定類別的型別,並顯示該型別所實行或繼承的所有介面。 若要編譯 Visual Basic 範例,請使用下列編譯器命令:

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

C#
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
 */

備註

GetInterfaces方法不會以特定順序傳回介面,例如字母順序或宣告順序。 您的程式碼不得相依于傳回介面的順序,因為該順序會有所不同。

如果目前的 Type 代表結構化泛型型別,這個方法會傳回 Type 類型參數取代為適當類型引數的物件。

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋介面條件約束,以及繼承自類別或介面條件約束的任何介面。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

另請參閱