當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。