类型是否实现了某个接口
2015-12-01 21:36
381 查看
定义下面的类和接口
使用IsAssignableFrom方法,确定指定类型的实例是否能分配给当前类型实例。IsAssignableFrom
如果满足下列任一条件,则为 true:
c and the current instance represent the same type.
c is derived either directly or indirectly from the current instance.
The current instance is an interface that c implements.
c is a generic type parameter, and the current instance represents one of the constraints of
c.
c represents a value type, and the current instance represents
Nullable<c> (Nullable(Of c) in Visual Basic),e.g.typeof(int?).IsAssignableFrom(typeof(int).
结果打印为true
使用GetInterface方法
结果打印为true
i.e.IsAssignableFrom doesn't go through reflection like GetInterface, so it should be orders faster.
interface Iccc { void dodo(); } class ccc : Iccc { public void dodo() { } }
使用IsAssignableFrom方法,确定指定类型的实例是否能分配给当前类型实例。IsAssignableFrom
public virtual bool IsAssignableFrom( Type c )
如果满足下列任一条件,则为 true:
c and the current instance represent the same type.
c is derived either directly or indirectly from the current instance.
The current instance is an interface that c implements.
c is a generic type parameter, and the current instance represents one of the constraints of
c.
c represents a value type, and the current instance represents
Nullable<c> (Nullable(Of c) in Visual Basic),e.g.typeof(int?).IsAssignableFrom(typeof(int).
Type type=typeof(ccc); if (typeof(Iccc).IsAssignableFrom(type)) { Console.WriteLine("true"); }
结果打印为true
使用GetInterface方法
if (type.GetInterface(typeof(Iccc).Name) != null) { Console.WriteLine("true"); }
结果打印为true
i.e.IsAssignableFrom doesn't go through reflection like GetInterface, so it should be orders faster.
相关文章推荐
- Linux 进程间通信--信号、TCP粘包处理
- Unique Binary Search Trees
- CentOS排错
- 网络流 poj 1698
- synchronized的作用
- 块设备学习
- 操作系统实验报告
- “南大软院大神养成计划“_第十六天的学习“
- android开发之Fragment利用Bundle保存状态
- 常量和变量
- 知其所以然之永不遗忘的算法
- 《leetCode》:Subsets II
- nyoj 171 聪明的kk【二维dp&&求最大值】
- 进制转换
- VS中工程的“依赖”,“库目录”,“包含目录”
- 代码细节的终极优化之循环展开、多路并行
- Lock的基本使用
- poj3070 Fibonacci
- DecimalFormat用法
- Linux系统忘了root密码