Type.GetInterfaces Metod

Definition

När det åsidosättas i en härledd klass, hämtar alla gränssnitt implementerade eller ärvda av den aktuella 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()

Returer

Type[]

En matris med Type objekt som representerar alla gränssnitt som implementerats eller ärvts av den aktuella Type.

-eller-

En tom matris av typen Type, om inga gränssnitt implementeras eller ärvs av den aktuella Type.

Implementeringar

Attribut

Undantag

En statisk initierare anropas och utlöser ett undantag.

Exempel

I följande exempel hämtas den angivna klassens typ och alla gränssnitt som typen implementerar eller ärver visas. Kompilera Visual Basic exempel genom att använda följande kompilatorkommandon:

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

Kommentarer

I .NET 6 och tidigare versioner returnerar metoden GetInterfaces inte gränssnitt i en viss ordning, till exempel alfabetisk ordning eller deklarationsordning. Koden får inte vara beroende av i vilken ordning gränssnitten returneras, eftersom den ordningen varierar. Från och med .NET 7 är ordningen deterministisk baserat på metadataordningen i sammansättningen.

Om strömmen Type representerar en konstruerad allmän typ returnerar den här metoden objekten Type med typparametrarna ersatta av lämpliga typargument.

Om den aktuella Type representerar en typparameter i definitionen av en allmän typ eller allmän metod söker den här metoden igenom gränssnittsbegränsningarna och eventuella gränssnitt som ärvts från klass- eller gränssnittsbegränsningar.

Gäller för

Se även