Type.GetInterface, méthode (String, Boolean) (System)

Changer de vue:
ScriptFree
Bibliothèque de classes .NET Framework
Type.GetInterface, méthode (String, Boolean)
Cet article a fait l'objet d'une traduction manuelle. Pour afficher simultanément cette page et le contenu source en anglais, choisissez le paramètre d'affichage Basse densité.

En cas de substitution dans une classe dérivée, recherche l'interface spécifiée, en indiquant s'il faut faire une recherche qui ne respecte pas la casse pour le nom de l'interface.

Espace de noms :  System
Assembly :  mscorlib (dans mscorlib.dll)
Syntaxe

Visual Basic
Public MustOverride Function GetInterface ( _
	name As String, _
	ignoreCase As Boolean _
) As Type
C#
public abstract Type GetInterface(
	string name,
	bool ignoreCase
)
VisualC++
public:
virtual Type^ GetInterface(
	String^ name, 
	bool ignoreCase
) abstract
F#
abstract GetInterface : 
        name:string * 
        ignoreCase:bool -> Type 

Paramètres

name
Type : System.String
Chaîne contenant le nom de l'interface à obtenir. Pour les interfaces génériques, il s'agit du nom tronqué.
ignoreCase
Type : System.Boolean
true pour ignorer le casse de cette partie de name qui spécifie le nom d'interface simple (la casse de la partie qui spécifie l'espace de noms doit être respectée).
ou
false pour effectuer une recherche qui respecte la casse de toutes les parties de name.

Valeur de retour

Type : System.Type
Objet qui représente l'interface ayant le nom spécifié, implémentée ou héritée par le Type actuel, s'il est trouvé ; sinon, null.

Implémentations

_Type.GetInterface(String, Boolean)
Exceptions

Exception Condition
ArgumentNullException

name est null.

AmbiguousMatchException

Le Type actuel représente un type qui implémente la même interface générique avec des arguments de type différents.

Notes

Le paramètre ignoreCase s'applique uniquement au nom d'interface simple, et pas à l'espace de noms. La casse de partie de name qui spécifie l'espace de noms doit être respectée, sans quoi l'interface ne sera pas trouvée. Par exemple, la chaîne "System.icomparable" recherche l'interface IComparable, mais pas la chaîne "system.icomparable".

Si le Type actuel représente un type générique construit, cette méthode retourne Type avec les paramètres de type remplacés par les arguments de type appropriés.

Si le Type actuel représente un paramètre de type dans la définition d'un type ou d'une méthode générique, cette méthode recherche les contraintes d'interface et toutes les interfaces héritées des contraintes de classe ou d'interface.

Remarque Remarque

Pour les interfaces génériques, le paramètre name est le nom tronqué, qui se termine par un accent grave (`) et le nombre de paramètres de type. Cela est vrai à la fois pour les définitions d'interfaces génériques et les interfaces génériques construites. Par exemple, pour rechercher IExample<T> (IExample(Of T) en Visual Basic) ou IExample<string> (IExample(Of String) en Visual Basic), recherchez "IExample`1".

Exemples

L'exemple de code suivant utilise la méthode GetInterface(String, Boolean) pour exécuter une recherche non sensible à la casse de la classe Hashtable pour l'interface IEnumerable.

L'exemple de code suivant illustre également la surcharge de méthode GetInterface(String) et la méthode GetInterfaceMap.

Visual Basic

   Public Shared Sub Main()
      Dim hashtableObj As New Hashtable()
      Dim objType As Type = hashtableObj.GetType()
      Dim arrayMemberInfo() As MemberInfo
      Dim arrayMethodInfo() As MethodInfo
      Try
         ' Get the methods implemented in 'IDeserializationCallback' interface.
         arrayMethodInfo = objType.GetInterface("IDeserializationCallback").GetMethods()
         Console.WriteLine(ControlChars.Cr + "Methods of 'IDeserializationCallback' Interface :")
         Dim index As Integer
         For index = 0 To arrayMethodInfo.Length - 1
            Console.WriteLine(arrayMethodInfo(index).ToString())
         Next index
         ' Get FullName for interface by using Ignore case search.
         Console.WriteLine(ControlChars.Cr + "Methods of 'IEnumerable' Interface")
         arrayMethodInfo = objType.GetInterface("ienumerable", True).GetMethods()
         For index = 0 To arrayMethodInfo.Length - 1
            Console.WriteLine(arrayMethodInfo(index).ToString())
         Next index
         'Get the Interface methods for 'IDictionary' interface
         Dim interfaceMappingObj As InterfaceMapping
         interfaceMappingObj = objType.GetInterfaceMap(GetType(IDictionary))
         arrayMemberInfo = interfaceMappingObj.InterfaceMethods
         Console.WriteLine(ControlChars.Cr + "Hashtable class Implements the following IDictionary Interface methods :")
         For index = 0 To arrayMemberInfo.Length - 1
            Console.WriteLine(arrayMemberInfo(index).ToString())
         Next index
      Catch e As Exception
         Console.WriteLine(("Exception : " + e.ToString()))
      End Try
   End Sub 'Main
End Class 'MyInterfaceClass 


C#

public static void Main()
{
    Hashtable hashtableObj = new Hashtable();
    Type objType = hashtableObj.GetType();
    MemberInfo[] arrayMemberInfo;
    MethodInfo[] arrayMethodInfo;
    try
    {   
        // Get the methods implemented in 'IDeserializationCallback' interface.
        arrayMethodInfo =objType.GetInterface("IDeserializationCallback").GetMethods();
        Console.WriteLine ("\nMethods of 'IDeserializationCallback' Interface :");
        for(int index=0;index < arrayMethodInfo.Length ;index++)
            Console.WriteLine (arrayMethodInfo[index].ToString() ); 

        // Get FullName for interface by using Ignore case search.
        Console.WriteLine ("\nMethods of 'IEnumerable' Interface");
        arrayMethodInfo = objType.GetInterface("ienumerable",true).GetMethods();
        for(int index=0;index < arrayMethodInfo.Length ;index++)
           Console.WriteLine (arrayMethodInfo[index].ToString()); 

        //Get the Interface methods for 'IDictionary' interface
        InterfaceMapping interfaceMappingObj;
        interfaceMappingObj = objType.GetInterfaceMap(typeof(IDictionary));
        arrayMemberInfo = interfaceMappingObj.InterfaceMethods;
        Console.WriteLine ("\nHashtable class Implements the following IDictionary Interface methods :");
        for(int index=0; index < arrayMemberInfo.Length; index++)
            Console.WriteLine (arrayMemberInfo[index].ToString() ); 
    }
    catch (Exception e)
    {
        Console.WriteLine ("Exception : " + e.ToString());            
    }                 
}


VisualC++

int main()
{
   Hashtable^ hashtableObj = gcnew Hashtable;
   Type^ objType = hashtableObj->GetType();
   array<MemberInfo^>^arrayMemberInfo;
   array<MethodInfo^>^arrayMethodInfo;
   try
   {
      // Get the methods implemented in 'IDeserializationCallback' interface.
      arrayMethodInfo = objType->GetInterface( "IDeserializationCallback" )->GetMethods();
      Console::WriteLine( "\nMethods of 'IDeserializationCallback' Interface :" );
      for ( int index = 0; index < arrayMethodInfo->Length; index++ )
         Console::WriteLine( arrayMethodInfo[ index ] );

      // Get FullName for interface by using Ignore case search.
      Console::WriteLine( "\nMethods of 'IEnumerable' Interface" );
      arrayMethodInfo = objType->GetInterface( "ienumerable", true )->GetMethods();
      for ( int index = 0; index < arrayMethodInfo->Length; index++ )
         Console::WriteLine( arrayMethodInfo[ index ] );

      //Get the Interface methods for 'IDictionary*' interface
      InterfaceMapping interfaceMappingObj;
      interfaceMappingObj = objType->GetInterfaceMap( IDictionary::typeid );
      arrayMemberInfo = interfaceMappingObj.InterfaceMethods;
      Console::WriteLine( "\nHashtable class Implements the following IDictionary Interface methods :" );
      for ( int index = 0; index < arrayMemberInfo->Length; index++ )
         Console::WriteLine( arrayMemberInfo[ index ] );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e );
   }
}


Informations de version

.NET Framework

Pris en charge dans : 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Pris en charge dans : 4, 3.5 SP1

Pris en charge dans :
Plateformes

Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows XP SP2 Édition x64, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2

Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
Voir aussi

Référence