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


C# Type.GetInterfaces()用法及代碼示例

Type.GetInterfaces()方法用於在派生類中重寫時,獲取由當前Type實現或繼承的所有接口。

用法: public abstract Type[] GetInterfaces ();

返回值:此方法返回一個Type對象數組,表示由當前Type實現或繼承的所有接口;如果當前Type沒有實現或繼承任何接口,則返回一個Type類型的空數組。


以下示例程序旨在說明Type.GetInterfaces()方法的使用:

示例1:

// C# program to demonstrate the 
// Type.GetInterfaces() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and intializing object of Type 
        Type objType = typeof(int); 
  
        // Getting interface of specified name 
        // using GetField(String) Method 
        Type[] minterface = objType.GetInterfaces(); 
  
        // Display the Result 
        Console.WriteLine("Interface present in type {0}", objType); 
        for (int i = 0; i < minterface.Length; i++) 
            Console.WriteLine(" {0}", minterface[i]); 
    } 
}
輸出:
Interface present in type System.Int32
 System.IFormattable
 System.IComparable
 System.IComparable`1[System.Int32]
 System.IConvertible
 System.IEquatable`1[System.Int32]

示例2:如果沒有定義公共領域

// C# program to demonstrate the 
// Type.GetInterfaces() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and intializing object of Type 
        Type objType = typeof(string); 
  
        // Getting interface of specified name 
        // using GetField(String) Method 
        Type[] minterface = objType.GetInterfaces(); 
  
        // Display the Result 
        Console.WriteLine("Interface present in type {0}", objType); 
        for (int i = 0; i < minterface.Length; i++) 
            Console.WriteLine(" {0}", minterface[i]); 
    } 
}
輸出:
Interface present in type System.String
 System.ICloneable
 System.Collections.Generic.IEnumerable`1[System.Char]
 System.IComparable
 System.IComparable`1[System.String]
 System.IConvertible
 System.Collections.IEnumerable
 System.IEquatable`1[System.String]

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetInterfaces() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。