Type.GetInterfaces Método

Definición

Cuando se invalida en una clase derivada, obtiene todas las interfaces implementadas o heredadas por el objeto actual 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()

Devoluciones

Type[]

Matriz de objetos que Type representa todas las interfaces implementadas o heredadas por el objeto actual Type.

O bien

Matriz vacía de tipo Type, si no hay interfaces implementadas o heredadas por el objeto actual Type.

Implementaciones

Atributos

Excepciones

Se invoca un inicializador estático y se produce una excepción.

Ejemplos

En el ejemplo siguiente se obtiene el tipo de la clase especificada y se muestran todas las interfaces que el tipo implementa o hereda. Para compilar el ejemplo de Visual Basic, use los siguientes comandos del 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

Comentarios

En .NET 6 y versiones anteriores, el método GetInterfaces no devuelve interfaces en un orden determinado, como el orden alfabético o de declaración. El código no debe depender del orden en el que se devuelven las interfaces, ya que ese orden varía. Sin embargo, a partir de .NET 7, el orden es determinista en función del orden de metadatos del ensamblado.

Si el objeto actual Type representa un tipo genérico construido, este método devuelve los objetos con los Type parámetros de tipo reemplazados por los argumentos de tipo adecuados.

Si el objeto actual Type representa un parámetro de tipo en la definición de un tipo genérico o un método genérico, este método busca en las restricciones de interfaz y las interfaces heredadas de las restricciones de clase o interfaz.

Se aplica a

Consulte también