Ler em inglês

Compartilhar via


Type.GetInterfaces Método

Definição

Quando substituído em uma classe derivada, obtém todas as interfaces implementadas ou herdadas pelo Type atual.

C#
public abstract Type[] GetInterfaces ();

Retornos

Type[]

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

  • ou - Uma matriz vazia do tipo Type, se nenhuma interface está implementada ou herdada pela Type atual.

Implementações

Exceções

Um inicializador estático é invocado e lança 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 Visual Basic, use os seguintes comandos do compilador:

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

Comentários

O GetInterfaces método 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.

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

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

Aplica-se a

Produto Versões
.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

Confira também