Type.GetInterfaces Metode

Definisi

Ketika ditimpa dalam kelas turunan, mendapatkan semua antarmuka yang diimplementasikan atau diwariskan oleh saat ini 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()

Mengembalikan

Type[]

Array Type objek yang mewakili semua antarmuka yang diimplementasikan atau diwarisi oleh .Type

-atau-

Array kosong dari jenis Type, jika tidak ada antarmuka yang diimplementasikan atau diwarisi oleh saat ini Type.

Penerapan

Atribut

Pengecualian

Penginisialisasi statis dipanggil dan melemparkan pengecualian.

Contoh

Contoh berikut mendapatkan jenis kelas yang ditentukan dan menampilkan semua antarmuka yang diterapkan atau diwarisi jenis. Untuk mengkompilasi contoh Visual Basic, gunakan perintah pengkompilasi berikut:

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

Keterangan

Dalam .NET 6 dan versi yang lebih lama, metode GetInterfaces tidak mengembalikan antarmuka dalam urutan tertentu, seperti urutan alfabet atau deklarasi. Kode Anda tidak boleh bergantung pada urutan pengembalian antarmuka, karena urutan tersebut bervariasi. Namun, dimulai dengan .NET 7, pemesanan bersifat deterministik berdasarkan urutan metadata dalam perakitan.

Jika saat ini Type mewakili jenis generik yang dibangun, metode ini mengembalikan Type objek dengan parameter jenis yang digantikan oleh argumen jenis yang sesuai.

Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, metode ini mencari batasan antarmuka dan antarmuka apa pun yang diwarisi dari batasan kelas atau antarmuka.

Berlaku untuk

Lihat juga