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
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)方法为IEnumerable接口执行不区分大小写的Hashtable类搜索。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
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 中), "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 "IExample1"`.

另请参阅

适用于