C# 中一些类关系的判定方法
1. IsAssignableFrom实例方法 判断一个类或者接口是否继承自另一个指定的类或者接口。
public interface IAnimal { }
public interface IDog : IAnimal { }
public class Dog : IDog { }
public class Cate : IAnimal { }
public class Parrot { }
var iAnimalType = typeof(IAnimal);
var iDogType = typeof(IDog);
var dogType = typeof(Dog);
var cateType = typeof(Cate);
var parrotType = typeof(Parrot); Console.WriteLine(iAnimalType.IsAssignableFrom(iDogType)
? $"{iDogType.Name} was inherited from {iAnimalType.Name}"
: $"{iDogType.Name} was not inherited from {iAnimalType.Name}"); Console.WriteLine(iAnimalType.IsAssignableFrom(dogType)
? $"{dogType.Name} was inherited from {iAnimalType.Name}"
: $"{dogType.Name} was not inherited from {iAnimalType.Name}"); Console.WriteLine(iDogType.IsAssignableFrom(dogType)
? $"{dogType.Name} was inherited from {iDogType.Name}"
: $"{dogType.Name} was not inherited from {iDogType.Name}"); Console.WriteLine(iAnimalType.IsAssignableFrom(cateType)
? $"{cateType.Name} was inherited from {iAnimalType.Name}"
: $"{cateType.Name} was not inherited from {iAnimalType.Name}"); Console.WriteLine(iAnimalType.IsAssignableFrom(parrotType)
? $"{parrotType.Name} inherited from {iAnimalType.Name}"
: $"{parrotType.Name} not inherited from {iAnimalType.Name}");
Console.ReadKey();
输出结果:
IDog was inherited from IAnimal
Dog was inherited from IAnimal
Dog was inherited from IDog
Cate was inherited from IAnimal
Parrot not inherited from IAnimal
2.IsInstanceOfType 判断某个对象是否继承自指定的类或者接口
Dog d=new Dog();
var result=typeof(IDog).IsInstanceOfType(d);
Console.WriteLine(result? $"Dog was inherited from IDog": $"Dog was not inherited from IDog");
Console.ReadKey();
输出结果:
Dog was inherited from IDog
3.IsSubclassOf 判断一个对象的类型是否继承自指定的类,不能用于接口的判断,这能用于判定类的关系
public interface IAnimal { }
public interface IDog : IAnimal { }
public class Dog : IDog { }
public class Husky : Dog { }
public class Cate : IAnimal { }
public class Parrot { }
Husky husky = new Husky();
var result = husky.GetType().IsSubclassOf(typeof(Dog));
Console.WriteLine(result ? $"Husky was inherited from Dog" : $"Husky was not inherited from Dog");
输出结果:
Husky was inherited from Dog
这个方法不能用于接口,如果穿接口进去永远返回的都是false
Dog dog = new Dog();
var dogResult = dog.GetType().IsSubclassOf(typeof(IDog));
Console.WriteLine(dogResult);
结果:
false
C# 中一些类关系的判定方法的更多相关文章
- C# 中一些类关系的判定方法 C#中关于增强类功能的几种方式 Asp.Net Core 轻松学-多线程之取消令牌
1. IsAssignableFrom实例方法 判断一个类或者接口是否继承自另一个指定的类或者接口. public interface IAnimal { } public interface ID ...
- python中的类,对象,方法,属性等介绍
注:这篇文章写得很好.加底纹的是我自己的理解 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...
- [python] 在 python2和3中关于类继承的 super方法简要说明
下面举一个例子,同样的代码使用 python2 和 python3 写的,大家注意两段程序中红色加粗的部分: python2的类继承使用super方法: #-*- coding:utf-8 -*- ' ...
- ES6中。类与继承的方法,以及与ES5中的方法的对比
// 在ES5中,通常使用构造函数方法去实现类与继承 // 创建父类 function Father(name, age){ this.name = name; this.age = age; } F ...
- Python 动态从文件中导入类或函数的方法
假设模块文件名是data_used_to_test.py,放在tests文件夹下 文件夹结构如下: project |-tests |-data_used_to_test.py 文件内包含一个test ...
- Python中自定义类未定义__lt__方法使用sort/sorted排序会怎么处理?
在<第8.23节 Python中使用sort/sorted排序与"富比较"方法的关系分析>中介绍了排序方法sort和函数sorted在没有提供key参数的情况下默认调用 ...
- 161018、springMVC中普通类获取注解service方法
1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.sp ...
- 总结:js中4类修改样式的方法
前言 最近在写一个扩展右键菜单的插件,既然是插件,想着一步到位,把相关的style样式设置都丢进js文件中,直接加载一个js文件便可以使用该插件,所以今天就研究了下js批量的插入样式的方法,即addS ...
- java中File类的常用所有方法及其应用
创建:createNewFile()在指定位置创建一个空文件,成功就返回true,如果已存在就不创建,然后返回false.mkdir() 在指定位置创建一个单级文件夹.mkdirs() 在指定位置 ...
随机推荐
- react源码总览(翻译)
用react也有段时间了, 是时候看看人家源码了. 看源码之前看到官方文档 有这么篇文章介绍其代码结构了, 为了看源码能顺利些, 遂决定将其翻译来看看, 小弟英语也是半瓢水, 好多单词得查词典, 不当 ...
- ORM之轻量级框架--Dapper
一.什么是Dapper? Dapper是一款轻量级Orm框架,它是属于半自动的,它和Entity Framework和NHibernate不同,它只有一个单文件,没有很复杂的配置,如果你喜欢原生Sql ...
- 记一次DDOS攻击防御实录
前言 笔者所在单位是一家小型创业公司,目前产品正在成长阶段,日活跃用户只有区区几万人次,并发只有日均 85/QPS,自建机房,带宽 100MB.在这样的背景下,完全没想过一个小产品会招来黑客的 ...
- MongoDB 运维相关的命令
1.在线释放内存 use admindb.runCommand({closeAllDatabases:1}) 注:3.2 版本 已经去掉了这个命令了 2.rs.status() 查询复制集状态 3.d ...
- SQL Server2008进程堵塞处理方法
进程堵塞处理方法: select * from sys.sysprocesses where blocked <>0 and DB_NAME(dbid)='GSHCPDB' ##查询堵 ...
- 禁止WPS2019开机自启动
最近开机发现WPS总是自己启动,导致开机速度会慢5s左右,从网上找了很多办法都没用,包括运行+msconfig,启动项里面没有WPS:杀毒软件自启动管理里面也没有找到WPS,百度了一圈也没有找到任何有 ...
- 最简单的Nginx讲解--HTTP服务器、正向代理、反向代理、负载均衡
1. Nginx 1.1 Nginx简介 Nginx是俄罗斯人开发,开源的,免费的. Nginx功能: 1) nginx作为http服务器:类似apache,tomcat,遵循http协议. a) 访 ...
- 判断HTML中的checkbox是否被选中
//合法性验证 function checkValidity() { var userNameCheck = $("#userNameCheck").attr('checked') ...
- 快速构建SPA框架SalutJS--项目工程目录 二
目录结构 上面这张图是salut的目录文档,从github上将其下载后直接运行node run和 node json 可以直接启动项目.下面逐个介绍每个目录的存放的文件和作用. constructio ...
- javascript对象和数组之 深拷贝和浅拷贝
管是在面试中还是我们的项目中经常会用到数组或者对象的深拷贝,下面我就自己总结的分享给大家. 首先要知道什么是深拷贝?什么是浅拷贝? 深拷贝:源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外 ...