Type.GetInterface Método

Definição

Obtém uma interface específica implementada ou herdada pelo atual Type.

Sobrecargas

Name Description
GetInterface(String, Boolean)

Quando sobrescrito numa classe derivada, procura a interface especificada, especificando se deve fazer uma pesquisa insensível a maiúsculas minúsculas pelo nome da interface.

GetInterface(String)

Procura a interface com o nome especificado.

GetInterface(String, Boolean)

Quando sobrescrito numa classe derivada, procura a interface especificada, especificando se deve fazer uma pesquisa insensível a maiúsculas minúsculas pelo nome da interface.

public:
 abstract Type ^ GetInterface(System::String ^ name, bool ignoreCase);
public abstract Type GetInterface(string name, bool ignoreCase);
abstract member GetInterface : string * bool -> Type
Public MustOverride Function GetInterface (name As String, ignoreCase As Boolean) As Type

Parâmetros

name
String

A cadeia que contém o nome da interface a obter. Para interfaces genéricas, este é o nome distorcido.

ignoreCase
Boolean

true ignorar o caso em que a parte de name que especifica o nome simples da interface (a parte que especifica o namespace deve ser corretamente maiúscula).

-ou-

false para realizar uma pesquisa sensível de maiúsculas e minúsculas para todas as partes de name.

Devoluções

Um objeto que representa a interface com o nome especificado, implementado ou herdado pelo atual Type, se encontrado; caso contrário, null.

Implementações

Exceções

name é null.

A corrente Type representa um tipo que implementa a mesma interface genérica com argumentos de tipo diferentes.

Exemplos

O exemplo de código seguinte utiliza o GetInterface(String, Boolean) método para realizar uma pesquisa insensível a maiúsculas e minúsculas da Hashtable classe para a IEnumerable interface.

O exemplo de código também demonstra a GetInterface(String) sobrecarga de métodos e o GetInterfaceMap método.

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

        // Get FullName for interface by using Ignore case search.
        Console.WriteLine ("\nMethods of 'IEnumerable' Interface");
        arrayMethodInfo = objType.GetInterface("ienumerable",true).GetMethods();
        foreach(MethodInfo methodInfo in arrayMethodInfo)
           Console.WriteLine (methodInfo);

        //Get the Interface methods for 'IDictionary' interface
        InterfaceMapping interfaceMappingOb = objType.GetInterfaceMap(typeof(IDictionary));
        arrayMemberInfo = interfaceMappingObj.InterfaceMethods;
        Console.WriteLine ("\nHashtable class Implements the following IDictionary Interface methods :");
        foreach(MemberInfo memberInfo in arrayMemberInfo)
           Console.WriteLine (memberInfo);
    }
    catch (Exception e)
    {
        Console.WriteLine ("Exception : " + e.ToString());
    }
}
let hashtableObj = Hashtable()
let objType = hashtableObj.GetType()
try
    // Get the methods implemented in 'IDeserializationCallback' interface.
    let arrayMethodInfo = objType.GetInterface("IDeserializationCallback").GetMethods()
    printfn "\nMethods of 'IDeserializationCallback' Interface :"
    for methodInfo in arrayMethodInfo do
        printfn $"{methodInfo}"

    // Get FullName for interface by using Ignore case search.
    printfn "\nMethods of 'IEnumerable' Interface"
    let arrayMethodInfo = objType.GetInterface("ienumerable",true).GetMethods()
    for methodInfo in arrayMethodInfo do
        printfn $"{methodInfo}"

    //Get the Interface methods for 'IDictionary' interface
    let interfaceMappingObj = objType.GetInterfaceMap typeof<IDictionary>
    let arrayMemberInfo = interfaceMappingObj.InterfaceMethods
    printfn "\nHashtable class Implements the following IDictionary Interface methods :"
    for memberInfo in arrayMemberInfo do
        printfn $"{memberInfo}"
with e ->
    printfn $"Exception : {e}"
   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
End Class

Observações

O ignoreCase parâmetro aplica-se apenas ao nome simples da interface, não ao namespace. A parte que name especifica o namespace deve ter o caso correto, caso contrário a interface não será encontrada. Por exemplo, a cadeia "System.icomparable" encontra a IComparable interface, mas a cadeia "system.icomparable" não.

Se o current Type representa um tipo genérico construído, esse método retorna o Type com os parâmetros type substituídos pelos argumentos de tipo apropriados.

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

Note

Para interfaces genéricas, o name parâmetro é o nome distorcido, terminando com um acento grave (') e o número de parâmetros de tipo. Isto é verdade tanto para definições genéricas de interfaces como para interfaces genéricas construídas. Por exemplo, para encontrar IExample<T> (IExample(Of T) em Visual Basic) ou IExample<string> (IExample(Of String) em Visual Basic), procure por "IExample`1".

Ver também

Aplica-se a

GetInterface(String)

Procura a interface com o nome especificado.

public:
 virtual Type ^ GetInterface(System::String ^ name);
public:
 Type ^ GetInterface(System::String ^ name);
public Type GetInterface(string name);
abstract member GetInterface : string -> Type
override this.GetInterface : string -> Type
member this.GetInterface : string -> Type
Public Function GetInterface (name As String) As Type

Parâmetros

name
String

A cadeia que contém o nome da interface a obter. Para interfaces genéricas, este é o nome distorcido.

Devoluções

Um objeto que representa a interface com o nome especificado, implementado ou herdado pelo atual Type, se encontrado; caso contrário, null.

Implementações

Exceções

name é null.

A corrente Type representa um tipo que implementa a mesma interface genérica com argumentos de tipo diferentes.

Exemplos

O exemplo de código seguinte usa o GetInterface(String) método para pesquisar a interface na Hashtable classe IDeserializationCallback e lista os métodos da interface.

O exemplo de código também demonstra a GetInterface(String, Boolean) sobrecarga de métodos e o GetInterfaceMap método.

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

        // Get FullName for interface by using Ignore case search.
        Console.WriteLine ("\nMethods of 'IEnumerable' Interface");
        arrayMethodInfo = objType.GetInterface("ienumerable",true).GetMethods();
        foreach(MethodInfo methodInfo in arrayMethodInfo)
           Console.WriteLine (methodInfo);

        //Get the Interface methods for 'IDictionary' interface
        InterfaceMapping interfaceMappingOb = objType.GetInterfaceMap(typeof(IDictionary));
        arrayMemberInfo = interfaceMappingObj.InterfaceMethods;
        Console.WriteLine ("\nHashtable class Implements the following IDictionary Interface methods :");
        foreach(MemberInfo memberInfo in arrayMemberInfo)
           Console.WriteLine (memberInfo);
    }
    catch (Exception e)
    {
        Console.WriteLine ("Exception : " + e.ToString());
    }
}
let hashtableObj = Hashtable()
let objType = hashtableObj.GetType()
try
    // Get the methods implemented in 'IDeserializationCallback' interface.
    let arrayMethodInfo = objType.GetInterface("IDeserializationCallback").GetMethods()
    printfn "\nMethods of 'IDeserializationCallback' Interface :"
    for methodInfo in arrayMethodInfo do
        printfn $"{methodInfo}"

    // Get FullName for interface by using Ignore case search.
    printfn "\nMethods of 'IEnumerable' Interface"
    let arrayMethodInfo = objType.GetInterface("ienumerable",true).GetMethods()
    for methodInfo in arrayMethodInfo do
        printfn $"{methodInfo}"

    //Get the Interface methods for 'IDictionary' interface
    let interfaceMappingObj = objType.GetInterfaceMap typeof<IDictionary>
    let arrayMemberInfo = interfaceMappingObj.InterfaceMethods
    printfn "\nHashtable class Implements the following IDictionary Interface methods :"
    for memberInfo in arrayMemberInfo do
        printfn $"{memberInfo}"
with e ->
    printfn $"Exception : {e}"
   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
End Class

Observações

A pesquisa diferencia name maiúsculas de minúsculas.

Se o current Type representa um tipo genérico construído, esse método retorna o Type com os parâmetros type substituídos pelos argumentos de tipo apropriados.

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

Note

Para interfaces genéricas, o name parâmetro é o nome distorcido, terminando com um acento grave (') e o número de parâmetros de tipo. Isto é verdade tanto para definições genéricas de interfaces como para interfaces genéricas construídas. Por exemplo, para encontrar IExample<T> (IExample(Of T) em Visual Basic) ou IExample<string> (IExample(Of String) em Visual Basic), procure por "IExample`1".

Ver também

Aplica-se a