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

最新文章

  1. Java Programming Test Question 1
  2. Linux基础01 学会使用命令帮助
  3. 构建linux内核源码树
  4. 转:LINQ查询返回DataTable类型
  5. 两种Makefile
  6. C#中一些易混淆概念总结
  7. 使用vue-cli构建多页面应用+vux(一)
  8. webrtc学习笔记2(Android端demo代码结构)
  9. CSS 样式书写规范
  10. FCC(ES6写法) Map the Debris
  11. Linux curl 网络访问
  12. java:给你一个数组和两个索引,交换下标为这两个索引的数字
  13. 通过PMP考试
  14. 20155321 《Java程序设计》实验三 敏捷开发与XP实践
  15. POS配置
  16. 如何利用Linux去油管下载带字幕的优质英文资料提升英文听力和词汇量
  17. 几分钟内学习 Clojure
  18. python调参神器hyperopt
  19. 微信小程序中 this.setData is not a function报错
  20. python 类和__class__理解

热门文章

  1. SpringCloud-服务注册与发现(注册中心)
  2. win10 DVWA下载安装配置(新手学渗透)
  3. 使用bat脚本永久激活Windows系统
  4. 开源的类似于Apache ab的压力测试命令行工具SuperBenchmarker
  5. Swashbuckle.AspNetCore3.0的二次封装与使用
  6. 技能提升丨Seacms 8.7版本SQL注入分析
  7. Fork/Join框架详解
  8. 【深度学习篇】--Seq2Seq模型从初识到应用
  9. Ubuntu16.04 部署配置GO语言开发环境 & 注意事项
  10. Android 性能测试优质实践汇总