C#判断某个类是否派生某个类或是否实现了某个接口
is和as
is关键字可以确定对象实例或表达式结果是否可转换为指定类型。基本语法:
expr is type
如果满足以下条件,则 is 语句为 true:
- expr 是与 type 具有相同类型的一个实例。
- expr 是派生自 type 的类型的一个实例。 换言之,expr 结果可以向上转换为 type 的一个实例。
- expr 具有属于 type 的一个基类的编译时类型,expr 还具有属于 type 或派生自 type 的运行时类型。 变量的编译时类型是其声明中定义的变量类型。 变量的运行时类型是分配给该变量的实例类型。
- expr 是实现 type 接口的类型的一个实例。
代码:
using System; public class Class1 : IFormatProvider
{
public object GetFormat(Type t)
{
if (t.Equals(this.GetType()))
return this;
return null;
}
} public class Class2 : Class1
{
public int Value { get; set; }
} public class Example
{
public static void Main()
{
var cl1 = new Class1();
Console.WriteLine(cl1 is IFormatProvider); //True
Console.WriteLine(cl1 is Object); //True
Console.WriteLine(cl1 is Class1); //True
Console.WriteLine(cl1 is Class2); //True
Console.WriteLine(); var cl2 = new Class2();
Console.WriteLine(cl2 is IFormatProvider); //True
Console.WriteLine(cl2 is Class2); //True
Console.WriteLine(cl2 is Class1); //True
Console.WriteLine(); Class1 cl = cl2;
Console.WriteLine(cl is Class1); //True
Console.WriteLine(cl is Class2); //True
}
}
as运算符类似于转换运算。如果无法进行转换,则 as 会返回 null,而不是引发异常。基本语法:
expr as type
等效
expr is type ? (type)expr : (type)null
可以尝试转换,根据转换的成功与否判断类的派生关系。
参考至:
- https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/is
- https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/as
Type.IsSubclassOf 和 Type.IsAssignableFrom
Type.IsSubclassOf 确定当前 Type 是否派生自指定的 Type。
[ComVisibleAttribute(true)]
public virtual bool IsSubclassOf(
Type c
)
如果当前 Type 派生于 c,则为 True;否则为 false。 如果 当前Type 和 c 相等,此方法也返回 True。
但是IsSubclassOf方法不能用于确定接口是否派生自另一个接口,或是否类实现的接口。
Type.IsAssignableFrom 确定指定类型的实例是否可以分配给当前类型的实例。
public virtual bool IsAssignableFrom(
Type c
)
如果满足下列任一条件,则为 true:
- c 且当前实例表示相同类型。
- c 是从当前实例直接或间接派生的。 c 它继承自的当前实例; 如果直接从当前实例派生 c 如果它继承自一个或多个从继承类的当前实例的一系列的当前实例中间接派生。
- 当前实例是一个 c 实现的接口。
- c 是一个泛型类型参数,并且当前实例表示 c 的约束之一。
代码:
using System; public interface IInterface
{
void Display();
} public class Class1 { } public class Implementation :Class1, IInterface
{
public void Display()
{
Console.WriteLine("The implementation...");
}
} public class Example
{
public static void Main()
{
Console.WriteLine("Implementation is a subclass of IInterface: {0}",
typeof(Implementation).IsSubclassOf(typeof(IInterface))); //False
Console.WriteLine("Implementation subclass of Class1: {0}",
typeof(Implementation).IsSubclassOf(typeof(Class1))); //True
Console.WriteLine("IInterface is assignable from Implementation: {0}",
typeof(IInterface).IsAssignableFrom(typeof(Implementation))); //True
Console.WriteLine("Class1 is assignable from Implementation: {0}",
typeof(Class1).IsAssignableFrom(typeof(Implementation))); //True
}
}
可以使用 Type.IsSubclassOf 判断类的派生, 使用 Type.IsAssignableFrom 判断类的派生和接口继承。
参考至:
- https://msdn.microsoft.com/zh-cn/library/office/system.type.issubclassof
- https://msdn.microsoft.com/zh-cn/library/office/system.type.isassignablefrom
C#判断某个类是否派生某个类或是否实现了某个接口的更多相关文章
- 那么类 Man 可以从类 Human 派生,类 Boy 可以从类 Man 派生
若在逻辑上 B 是 A 的“一种”(a kind of ),则允许 B 继承 A 的功 能和属性. 例如男人(Man)是人(Human)的一种,男孩(Boy)是男人的一种. 那么类 Man 可以从类 ...
- 从Student类和Teacher类多重派生Graduate类 代码参考
#include <iostream> #include <cstring> using namespace std; class Person { private: char ...
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- 【转载】C++中的基类与派生类
转自:http://www.cnblogs.com/sujz/articles/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的 ...
- (转) C++中基类和派生类之间的同名函数的重载问题
下面有关派生类与基类中存在同名函数 fn: class A { public: void fn() {} void fn(int a) {} }; class B : public A { publi ...
- C++ Pirmer : 第十五章 : 面向对象程序设计之基类和派生的定义、类型转换与继承与虚函数
基类和派生类的定义以及虚函数 基类Quote的定义: classs Quote { public: Quote() = default; Quote(cosnt std::string& bo ...
- c++中基类与派生类中隐含的this指针的分析
先不要看结果,看一下你是否真正了解了this指针? #include<iostream> using namespace std; class Parent{ public: int x; ...
- C++学习21 基类和派生类的赋值
在C/C++中,经常会发生数据类型转换,例如整型数据可以赋值给浮点型变量,在赋值之前,先把整型数据转换为浮点型:反过来,浮点型数据也可以赋值给整型变量. 数据类型转换的前提是,编译器知道如何对数据进行 ...
- C++中的基类与派生类
派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的成员被派生类继承后,保持原来的状态 private 基类的public和protected的成员被派生类继 ...
随机推荐
- 使用MyBatis集成阿里巴巴druid连接池(不使用spring)
在工作中发现mybatis默认的连接池POOLED,运行时间长了会报莫名其妙的连接失败错误.因此采用阿里巴巴的Druid数据源(码云链接 ,中文文档链接). mybatis更多数据源参考博客链接 . ...
- uva211 回溯
大致题意:每个多米诺骨牌可能横着,也可能竖着,请你判断有哪些合法的摆放方式. 这题的dfs需要注意一下,不能以某个点直接开始延伸,如果这样延伸可能会无法到达终点(也就是遍历全图).我的dfs方法就是枚 ...
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
题意: 起初定28张卡牌的排列,把其中11, 21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...
- Hadoop 错误归档库
在hive中操作任意mapreduce相关语句 The size of Container logs revealed the below error: 2015-04-24 11:41:41,858 ...
- android判断网络是否可用
private boolean isNetworkConnected(Context context) { ConnectivityMannger cManager = (ConnectivityMa ...
- javascript对象(简略)
javascript对象有着自有的属性,对象可以从一个称为原型的对象继承属性,对象的方法通常是继承的属性,原型式继承是javascript的核心特征.
- linux下编译sphinx拓展
编译libsphinxclient sphinx 源码包里的api文件夹下的libsphinxclient cd /root/api/libsphinxclient/ ./configure make ...
- 【memcache】windos下 memcache更改默认的端口和最大使用内存
1>用内网ip的方式提供web应用服务器调用,不允许直接通过外网调用,如将memcache服务器放在192.168.1.55的服务器上 2>修改端口,如改为11200 3>分配内存, ...
- FusionWidgets之AngularGauge图
1.设置AngularGauge图的数据源 AngularGauge.xml: <?xml version="1.0" encoding="UTF-8"? ...
- Invalid property 'url' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]
1.错误描述 INFO:2015-05-01 13:13:05[localhost-startStop-1] - Initializing c3p0-0.9.2.1 [built 20-March-2 ...