Type.GetInterface 方法

定义

获取由当前 Type 实现或继承的特定接口。

重载

GetInterface(String)

搜索具有指定名称的接口。

GetInterface(String, Boolean)

当在派生类中重写时,搜索指定的接口,指定是否要对接口名称执行不区分大小写的搜索。

GetInterface(String)

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

搜索具有指定名称的接口。

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

参数

name
String

包含要获取的接口名称的字符串。 对于泛型接口,这是重整名称。

返回

表示具有指定名称且由当前的 Type 实现或继承的接口的对象(如果找到的话);否则为 null

实现

例外

namenull

当前 Type 表示实现具有不同类型参数的相同泛型接口的类型。

示例

下面的代码示例使用 GetInterface(String) 方法在 类IDeserializationCallbackHashtable搜索 接口,并列出 接口的方法。

该代码示例还演示了 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 参数是整型名称,以严重重音 (') 和类型参数数结尾。 这适用于泛型接口定义和构造的泛型接口。 例如,若要IExample<T>IExample(Of T)在 Visual Basic) 或 IExample<string> Visual Basic) 中查找 (IExample(Of String) (,请搜索 "IExample`1"

另请参阅

适用于

.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)

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

当在派生类中重写时,搜索指定的接口,指定是否要对接口名称执行不区分大小写的搜索。

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

参数

name
String

包含要获取的接口名称的字符串。 对于泛型接口,这是重整名称。

ignoreCase
Boolean

true 表示对于用来指定简单接口名称的 name 部分(该部分指定命名空间大小写必须正确)忽略其大小写。

false 表示对 name 的所有部分执行区分大小写的搜索。

返回

表示具有指定名称且由当前的 Type 实现或继承的接口的对象(如果找到的话);否则为 null

实现

例外

namenull

当前 Type 表示实现具有不同类型参数的相同泛型接口的类型。

示例

下面的代码示例使用 GetInterface(String, Boolean) 方法对 接口的 HashtableIEnumerable 执行不区分大小写的搜索。

该代码示例还演示了 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 参数是整型名称,以严重重音 (') 和类型参数数结尾。 这适用于泛型接口定义和构造的泛型接口。 例如,若要IExample<T>IExample(Of T)在 Visual Basic) 或 IExample<string> Visual Basic) 中查找 (IExample(Of String) (,请搜索 "IExample`1"

另请参阅

适用于

.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