Type.GetInterface 方法

定義

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

多載

GetInterface(String)

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

GetInterface(String, Boolean)

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

GetInterface(String)

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

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

C#
public Type? GetInterface (string name);
C#
public Type GetInterface (string name);

參數

name
String

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

傳回

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

實作

例外狀況

namenull

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

範例

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

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

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

備註

搜尋 name 會區分大小寫。

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

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

備註

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

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetInterface(String, Boolean)

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

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

C#
public abstract Type? GetInterface (string name, bool ignoreCase);
C#
public abstract Type GetInterface (string name, bool ignoreCase);

參數

name
String

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

ignoreCase
Boolean

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

-或-

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

傳回

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

實作

例外狀況

namenull

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

範例

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

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

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

備註

參數 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>

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1