Type.GetInterface Type.GetInterface Type.GetInterface Type.GetInterface Method

定义

获取由当前 Type 实现或继承的特定接口。Gets a specific interface implemented or inherited by the current Type.

重载

GetInterface(String) GetInterface(String) GetInterface(String) GetInterface(String)

搜索具有指定名称的接口。Searches for the interface with the specified name.

GetInterface(String, Boolean) GetInterface(String, Boolean) GetInterface(String, Boolean) GetInterface(String, Boolean)

当在派生类中重写时,搜索指定的接口,指定是否要对接口名称执行不区分大小写的搜索。When overridden in a derived class, searches for the specified interface, specifying whether to do a case-insensitive search for the interface name.

GetInterface(String) GetInterface(String) GetInterface(String) GetInterface(String)

搜索具有指定名称的接口。Searches for the interface with the specified name.

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

参数

name
String String String String

包含要获取的接口名称的字符串。The string containing the name of the interface to get. 对于泛型接口,这是重整名称。For generic interfaces, this is the mangled name.

返回

表示具有指定名称且由当前的 Type 实现或继承的接口的对象(如果找到的话);否则为 nullAn object representing the interface with the specified name, implemented or inherited by the current Type, if found; otherwise, null.

实现

异常

当前 Type 表示实现具有不同类型参数的相同泛型接口的类型。The current Type represents a type that implements the same generic interface with different type arguments.

示例

下面的代码示例使用GetInterface(String)方法搜索HashtableIDeserializationCallback接口,并列出了接口的方法。The following code example uses the GetInterface(String) method to search the Hashtable class for the IDeserializationCallback interface, and lists the methods of the interface.

代码示例还演示GetInterface(String, Boolean)方法重载和GetInterfaceMap方法。The code example also demonstrates the GetInterface(String, Boolean) method overload and the GetInterfaceMap method.

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 );
   }
}
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());            
    }                 
}
   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 

注解

搜索name区分大小写。The search for name is case-sensitive.

如果当前Type表示构造泛型类型,此方法返回Type具有类型参数替换为适当的类型参数。If the current Type represents a constructed generic type, this method returns the Type with the type parameters replaced by the appropriate type arguments.

如果当前Type表示类型参数的泛型类型或泛型方法,则此方法定义中搜索接口约束和继承的类或接口约束的所有接口。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

备注

对于泛型接口,name参数是重整的名称,结尾的重读符号 (`) 和类型参数的数目。For generic interfaces, the name parameter is the mangled name, ending with a grave accent (`) and the number of type parameters. 这适用于泛型接口定义和构造的泛型接口。This is true for both generic interface definitions and constructed generic interfaces. 例如,若要查找IExample<T>(IExample(Of T)在 Visual Basic 中) 或IExample<string>(IExample(Of String)在 Visual Basic 中),搜索"IExample`1"For example, to find IExample<T> (IExample(Of T) in Visual Basic) or IExample<string> (IExample(Of String) in Visual Basic), search for "IExample`1".

另请参阅

GetInterface(String, Boolean) GetInterface(String, Boolean) GetInterface(String, Boolean) GetInterface(String, Boolean)

当在派生类中重写时,搜索指定的接口,指定是否要对接口名称执行不区分大小写的搜索。When overridden in a derived class, searches for the specified interface, specifying whether to do a case-insensitive search for the interface name.

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

参数

name
String String String String

包含要获取的接口名称的字符串。The string containing the name of the interface to get. 对于泛型接口,这是重整名称。For generic interfaces, this is the mangled name.

ignoreCase
Boolean Boolean Boolean Boolean

true 表示对于用来指定简单接口名称的 name 部分(该部分指定命名空间大小写必须正确)忽略其大小写。true to ignore the case of that part of name that specifies the simple interface name (the part that specifies the namespace must be correctly cased).

- 或 --or- false 表示对 name 的所有部分执行区分大小写的搜索。false to perform a case-sensitive search for all parts of name.

返回

表示具有指定名称且由当前的 Type 实现或继承的接口的对象(如果找到的话);否则为 nullAn object representing the interface with the specified name, implemented or inherited by the current Type, if found; otherwise, null.

实现

异常

当前 Type 表示实现具有不同类型参数的相同泛型接口的类型。The current Type represents a type that implements the same generic interface with different type arguments.

示例

下面的代码示例使用GetInterface(String, Boolean)方法来执行不区分大小写的搜索HashtableIEnumerable接口。The following code example uses the GetInterface(String, Boolean) method to perform a case-insensitive search of the Hashtable class for the IEnumerable interface.

代码示例还演示GetInterface(String)方法重载和GetInterfaceMap方法。The code example also demonstrates the GetInterface(String) method overload and the GetInterfaceMap method.

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 );
   }
}
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());            
    }                 
}
   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 

注解

ignoreCase参数仅适用于简单接口名称,不到命名空间。The ignoreCase parameter applies only to the simple interface name, not to the namespace. 部分name指定命名空间必须具有正确的大小写,或将找不到该接口。The portion of name that specifies the namespace must have the correct case, or the interface will not be found. 例如,"System.icomparable"找到该字符串IComparable接口,而字符串"system.icomparable"不工作。For example, the string "System.icomparable" finds the IComparable interface, but the string "system.icomparable" does not.

如果当前Type表示构造泛型类型,此方法返回Type具有类型参数替换为适当的类型参数。If the current Type represents a constructed generic type, this method returns the Type with the type parameters replaced by the appropriate type arguments.

如果当前Type表示类型参数的泛型类型或泛型方法,则此方法定义中搜索接口约束和继承的类或接口约束的所有接口。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

备注

对于泛型接口,name参数是重整的名称,结尾的重读符号 (`) 和类型参数的数目。For generic interfaces, the name parameter is the mangled name, ending with a grave accent (`) and the number of type parameters. 这适用于泛型接口定义和构造的泛型接口。This is true for both generic interface definitions and constructed generic interfaces. 例如,若要查找IExample<T>(IExample(Of T)在 Visual Basic 中) 或IExample<string>(IExample(Of String)在 Visual Basic 中),搜索"IExample1"。For example, to find IExample<T> (IExample(Of T) in Visual Basic) or IExample<string> (IExample(Of String) in Visual Basic), search for "IExample1"`.

另请参阅

适用于