Type.GetInterface 方法

定義

取得由目前 Type 所實作或繼承的特定介面。

多載

GetInterface(String)

搜尋具有指定名稱的介面。

GetInterface(String, Boolean)

在衍生類別中覆寫時,指定是否針對介面名稱以不區分大小寫的方式來搜尋指定的介面。

GetInterface(String)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

搜尋具有指定名稱的介面。

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

參數

name
String

字串,包含要取得的介面名稱。 若為泛型介面,則為 mangled 名稱。

傳回

物件,代表具有指定之名稱的介面是由目前 Type 所實作或繼承的 (如有找到),否則為 null

實作

例外狀況

namenull

目前的 Type 代表實作具有不同型別引數之相同泛型介面的類型。

範例

下列程式碼範例會 GetInterface(String) 使用 方法來搜尋 Hashtable 介面的 類別 IDeserializationCallback ,並列出 介面的方法。

程式碼範例也會示範 GetInterface(String, Boolean) 方法多載和 GetInterfaceMap 方法。

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

備註

搜尋 name 會區分大小寫。

如果目前的 Type 代表建構的泛型型別,這個方法會傳回 Type ,並將 型別參數取代為適當的型別引數。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋介面條件約束,以及繼承自類別或介面條件約束的任何介面。

注意

若為泛型介面,參數 name 是泛型名稱,結尾為輔色 (') 和類型參數數目。 這適用于泛型介面定義和建構的泛型介面。 例如,若要在 Visual Basic) 中尋找 (,或在 Visual Basic) 中尋找 IExample<T>IExample(Of T) (IExample(Of String) ,請搜尋 "IExample`1"IExample<string>

另請參閱

適用於

GetInterface(String, Boolean)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

在衍生類別中覆寫時,指定是否針對介面名稱以不區分大小寫的方式來搜尋指定的介面。

public:
 abstract Type ^ GetInterface(System::String ^ name, bool ignoreCase);
public abstract Type? GetInterface (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

字串,包含要取得的介面名稱。 若為泛型介面,則為 mangled 名稱。

ignoreCase
Boolean

true,便會忽略 name 中指定簡單介面名稱之該部分的大小寫 (指定命名空間的部分必須使用正確的大小寫)。

-或-

false,便會針對 name 的所有部分執行區分大小寫的搜尋。

傳回

物件,代表具有指定之名稱的介面是由目前 Type 所實作或繼承的 (如有找到),否則為 null

實作

例外狀況

namenull

目前的 Type 代表實作具有不同型別引數之相同泛型介面的類型。

範例

下列程式碼範例會 GetInterface(String, Boolean) 使用 方法,針對 IEnumerable 介面執行類別不區分大小寫的 Hashtable 搜尋。

程式碼範例也會示範 GetInterface(String) 方法多載和 GetInterfaceMap 方法。

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

備註

參數 ignoreCase 只適用于簡單介面名稱,不適用於 命名空間。 指定命名空間的 name 部分必須有正確的大小寫,否則找不到 介面。 例如,字串 「System.icomparable」 會尋找 IComparable 介面,但字串 「system.icomparable」 則不會。

如果目前的 Type 代表建構的泛型型別,這個方法會傳回 Type ,並將 型別參數取代為適當的型別引數。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋介面條件約束,以及繼承自類別或介面條件約束的任何介面。

注意

若為泛型介面,參數 name 是泛型名稱,結尾為輔色 (') 和類型參數數目。 這適用于泛型介面定義和建構的泛型介面。 例如,若要在 Visual Basic) 中尋找 (,或在 Visual Basic) 中尋找 IExample<T>IExample(Of T) (IExample(Of String) ,請搜尋 "IExample`1"IExample<string>

另請參閱

適用於