語言

Type.GetInterface 方法

定義

讓特定介面實作或繼承於當前 Type的 。

多載

名稱 Description
GetInterface(String)

搜尋指定名稱的介面。

GetInterface(String, Boolean)

當在派生類別中覆寫時,會搜尋指定的介面,並指定是否對介面名稱進行大小寫不區分的搜尋。

GetInterface(String)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

搜尋指定名稱的介面。

public:
 Type ^ GetInterface(System::String ^ name);
public:
 virtual Type ^ GetInterface(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)]
public Type? GetInterface(string name);
public Type? GetInterface(string name);
public Type GetInterface(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)>]
member this.GetInterface : string -> Type
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

包含要取得的介面名稱的字串。 對於通用介面來說,這就是混亂的名稱。

傳回

一個代表指定名稱介面的物件,若找到則由目前 Type的 實作或繼承;否則,為 null。

實作

屬性

例外狀況

name 是 null。

電流 Type 代表一種實作相同通用介面但參數不同型別的型別。

範例

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

程式碼範例同時展示了GetInterface(String, Boolean)方法過載與方法。GetInterfaceMap

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 代表一般型別或通用方法定義中的型別參數,該方法會搜尋介面約束以及從類別或介面約束繼承的任何介面。

Note

對於通用介面, name 參數是混亂的名稱,結尾帶有重音符號('),以及型別參數的數量。 這對通用介面定義和構造化通用介面皆適用。 例如,要找到 IExample<T>(IExample(Of T) 在 Visual Basic 中)或 IExample<string>(IExample(Of String)在 Visual Basic中),搜尋 "IExample`1"。

另請參閱

適用於

GetInterface(String, Boolean)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

當在派生類別中覆寫時,會搜尋指定的介面,並指定是否對介面名稱進行大小寫不區分的搜尋。

public:
 abstract Type ^ GetInterface(System::String ^ name, bool ignoreCase);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)]
public abstract Type? GetInterface(string name, bool ignoreCase);
public abstract Type? GetInterface(string name, bool ignoreCase);
public abstract Type GetInterface(string name, bool ignoreCase);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)>]
abstract member GetInterface : string * bool -> Type
abstract member GetInterface : string * bool -> Type
Public MustOverride Function GetInterface (name As String, ignoreCase As Boolean) As Type

參數

name
String

包含要取得的介面名稱的字串。 對於通用介面來說,這就是混亂的名稱。

ignoreCase
Boolean

true 忽略該部分 name 的大小寫,則指定了簡單的介面名稱(指定命名空間的部分必須正確大小寫)。

-或-

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

傳回

一個代表指定名稱介面的物件,若找到則由目前 Type的 實作或繼承;否則,為 null。

實作

屬性

例外狀況

name 是 null。

電流 Type 代表一種實作相同通用介面但參數不同型別的型別。

範例

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

程式碼範例同時展示了GetInterface(String)方法過載與方法。GetInterfaceMap

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 代表一般型別或通用方法定義中的型別參數,該方法會搜尋介面約束以及從類別或介面約束繼承的任何介面。

Note

對於通用介面, name 參數是混亂的名稱,結尾帶有重音符號('),以及型別參數的數量。 這對通用介面定義和構造化通用介面皆適用。 例如,要找到 IExample<T>(IExample(Of T) 在 Visual Basic 中)或 IExample<string>(IExample(Of String)在 Visual Basic中),搜尋 "IExample`1"。

另請參閱

適用於