当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Type.GetInterface()用法及代码示例


Type.GetInterface()方法用于获取由当前Type实现或继承的特定接口。

  • GetInterface(String)方法
  • GetInterface(String,Boolean)方法

GetInterface(String) Method

此方法用于搜索具有指定名称的接口。


用法: public Type GetInterface (string name);
Here, it takes the string containing the name of the interface to get. For generic interfaces, this is the mangled name.

返回值:此方法返回一个对象,该对象表示具有指定名称的接口,该对象由当前Type实现或继承,否则,返回null。

异常:如果名称为null,则此方法抛出ArgumentNullException。

以下示例程序旨在说明Type.GetInterface(String)方法的用法:

示例1:

// C# program to demonstrate the 
// Type.GetInterface(String) Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(int); 
  
        // try-catch block for handling Exception 
        try { 
  
            // Getting interface of specified name 
            // using GetField(String) Method 
            Type minterface = objType.GetInterface("IFormattable"); 
  
            // Display the Result 
            Console.WriteLine("interface is: {0}", minterface); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e)  
        { 
            Console.Write("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
interface is: System.IFormattable

示例2:对于ArgumentNullException

// C# program to demonstrate the 
// Type.GetInterface(String) Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(int); 
  
        // try-catch block for handling Exception 
        try { 
  
            // Getting interface of specified name 
            // using GetField(String) Method 
            Type minterface = objType.GetInterface(null); 
  
            // Display the Result 
            Console.WriteLine("interface is: {0}", minterface); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e)  
        { 
            Console.WriteLine("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
name is null.
Exception Thrown: System.ArgumentNullException

GetInterface(String, Boolean) Method

此方法用于搜索指定的接口,指定在派生类中被覆盖时是否对接口名称进行大小写敏感搜索。

用法: public abstract Type GetInterface (string name, bool ignoreCase);

参数:

name:包含要获取的接口名称的字符串。对于通用接口,这是错误的名称。

ignoreCase:如果忽略名称的指定简单接口名称的部分(指定名称空间的部分必须正确区分大小写)的大小写,则为true;否则,对名称的所有部分进行区分大小写的搜索为false。

返回值:此方法返回n对象,该对象代表具有指定名称的接口,该对象由当前Type实现或继承,如果找到,则为null。

异常:如果name为null,则此方法将引发ArgumentNullException。

以下示例程序旨在说明上述方法的用法:

示例1:

// C# program to demonstrate the 
// Type.GetInterface(String,  
// Boolean) Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(int); 
  
        // try-catch block for handling Exception 
        try { 
  
            // Getting interface of specified name 
            // using GetField(String) Method 
            Type minterface = objType.GetInterface("iformattable", true); 
  
            // Display the Result 
            Console.WriteLine("interface is: {0}", minterface); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e) { 
            Console.WriteLine("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
interface is: System.IFormattable

示例2:对于ArgumentNullException

// C# program to demonstrate the 
// Type.GetInterface(String,  
// Boolean) Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(int); 
  
        // try-catch block for handling Exception 
        try { 
  
            // Getting interface of specified name 
            // using GetField(String) Method 
            Type minterface = objType.GetInterface(null, true); 
  
            // Display the Result 
            Console.WriteLine("interface is: {0}", minterface); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e)  
        { 
            Console.WriteLine("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}

输出:

name is null.
Exception Thrown: System.ArgumentNullException

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Type.GetInterface() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。