Type.GetInterfaces Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
En cas de substitution dans une classe dérivée, obtient toutes les interfaces implémentées ou héritées par le Typeactuel.
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()
Retours
Tableau d’objets Type représentant toutes les interfaces implémentées ou héritées par le courant Type.
- ou -
Tableau vide de type Type, si aucune interface n’est implémentée ou héritée par le tableau actuel Type.
Implémente
- Attributs
Exceptions
Un initialiseur statique est appelé et lève une exception.
Exemples
L’exemple suivant obtient le type de la classe spécifiée et affiche toutes les interfaces que le type implémente ou hérite. Pour compiler l’exemple Visual Basic, utilisez les commandes du compilateur suivantes :
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
Remarques
Dans .NET 6 et versions antérieures, la méthode GetInterfaces ne retourne pas d’interfaces dans un ordre particulier, tel que l’ordre alphabétique ou de déclaration. Votre code ne doit pas dépendre de l’ordre dans lequel les interfaces sont retournées, car cet ordre varie. Toutefois, à partir de .NET 7, l’ordre est déterministe en fonction de l’ordre des métadonnées dans l’assembly.
Si le courant Type représente un type générique construit, cette méthode retourne les objets avec les Type paramètres de type remplacés par les arguments de type appropriés.
Si le paramètre actuel Type représente un paramètre de type dans la définition d’un type générique ou d’une méthode générique, cette méthode recherche les contraintes d’interface et toutes les interfaces héritées de contraintes de classe ou d’interface.