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. 若為泛型介面,則為 mangled 名稱。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)方法來搜尋Hashtable類別IDeserializationCallback介面,並列出介面的方法。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. 這是泛型介面定義和建構的泛型介面,則為 true。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. 若為泛型介面,則為 mangled 名稱。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)方法來執行不區分大小寫的搜尋Hashtable類別IEnumerable介面。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. 這是泛型介面定義和建構的泛型介面,則為 true。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"`.

另請參閱

適用於