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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。