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. 這適用于泛型介面定義和結構化泛型介面。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)方法,針對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 '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.servicemodel" 會尋找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"`.

另請參閱

適用於