Type.GetInterfaces Método

Definição

Quando sobrescrito numa classe derivada, obtém todas as interfaces implementadas ou herdadas pelo atual Type.

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

Devoluções

Type[]

Um array de Type objetos que representa todas as interfaces implementadas ou herdadas pelo atual Type.

-ou-

Um array vazio do tipo Type, se não houver interfaces implementadas ou herdadas pelo atual Type.

Implementações

Exceções

Um inicializador estático é invocado e lança uma exceção.

Exemplos

O exemplo seguinte obtém o tipo da classe especificada e mostra todas as interfaces que o tipo implementa ou herda. Para compilar o exemplo do Visual Basic, use os seguintes comandos do compilador:

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

Observações

Nas versões .NET 6 e anteriores, o método GetInterfaces não devolve interfaces numa ordem específica, como ordem alfabética ou de declaração. O teu código não deve depender da ordem em que as interfaces são devolvidas, porque essa ordem varia. No entanto, a partir do .NET 7, a ordenação é determinística com base na ordenação dos metadados na assembleia.

Se a corrente Type representa um tipo genérico construído, este método devolve os Type objetos com os parâmetros de tipo substituídos pelos argumentos de tipo apropriados.

Se a corrente Type representa um parâmetro de tipo na definição de um tipo genérico ou método genérico, este método pesquisa as restrições de interface e quaisquer interfaces herdadas de restrições de classe ou interface.

Aplica-se a

Ver também