Type.GetInterfaces Método

Definição

Quando substituído em uma 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()

Retornos

Type[]

Uma matriz de Type objetos que representa todas as interfaces implementadas ou herdadas pelo atual Type.

-ou-

Uma matriz vazia de tipo Type, se nenhuma interface for implementada ou herdada pelo atual Type.

Implementações

Exceções

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

Exemplos

O exemplo a seguir obtém o tipo da classe especificada e exibe todas as interfaces que o tipo implementa ou herda. Para compilar o exemplo de 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

Comentários

Em .NET 6 e versões anteriores, o método GetInterfaces não retorna interfaces em uma ordem específica, como ordem alfabética ou de declaração. Seu código não deve depender da ordem na qual as interfaces são retornadas, pois essa ordem varia. No entanto, começando com .NET 7, a ordenação é determinística com base na ordenação de metadados no assembly.

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

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

Aplica-se a

Confira também