获取当前类型在 C# 中实现或继承的所有接口
如果要获取当前 Type 实现或继承的所有接口,可以编写如下代码 −
示例
using System;
public class Demo {
public static void Main() {
Type type = typeof(float);
Type myInterface = type.GetInterface("IFormattable",true);
Type[] myInterfaces = type.GetInterfaces();
Console.WriteLine("Interface = "+myInterface);
Console.WriteLine("All the Interfaces...");
for (int i = 0; i < myInterfaces.Length; i++)
Console.WriteLine(""+myInterfaces[i]);
}
}输出
该代码将生成以下输出 −
Interface = System.IFormattable All the Interfaces... System.IComparable System.IFormattable System.IConvertible System.IComparable`1[System.Single] System.IEquatable`1[System.Single]
我们再看一个示例 −
示例
using System;
public class Demo {
public static void Main() {
Type type = typeof(int);
Type myInterface = type.GetInterface("IFormattable");
Type[] myInterfaces = type.GetInterfaces();
Console.WriteLine("Interface = "+myInterface);
Console.WriteLine("All the Interfaces...");
for (int i = 0; i < myInterfaces.Length; i++)
Console.WriteLine(""+myInterfaces[i]);
}
}输出
该代码将生成以下输出 −
Interface = System.IFormattable All the Interfaces... System.IComparable System.IFormattable System.IConvertible System.IComparable`1[System.Int32] System.IEquatable`1[System.Int32]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP