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# 中一些类关系的判定方法的更多相关文章

  1. C# 中一些类关系的判定方法 C#中关于增强类功能的几种方式 Asp.Net Core 轻松学-多线程之取消令牌

    1.  IsAssignableFrom实例方法 判断一个类或者接口是否继承自另一个指定的类或者接口. public interface IAnimal { } public interface ID ...

  2. python中的类,对象,方法,属性等介绍

    注:这篇文章写得很好.加底纹的是我自己的理解 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...

  3. [python] 在 python2和3中关于类继承的 super方法简要说明

    下面举一个例子,同样的代码使用 python2 和 python3 写的,大家注意两段程序中红色加粗的部分: python2的类继承使用super方法: #-*- coding:utf-8 -*- ' ...

  4. ES6中。类与继承的方法,以及与ES5中的方法的对比

    // 在ES5中,通常使用构造函数方法去实现类与继承 // 创建父类 function Father(name, age){ this.name = name; this.age = age; } F ...

  5. Python 动态从文件中导入类或函数的方法

    假设模块文件名是data_used_to_test.py,放在tests文件夹下 文件夹结构如下: project |-tests |-data_used_to_test.py 文件内包含一个test ...

  6. Python中自定义类未定义__lt__方法使用sort/sorted排序会怎么处理?

    在<第8.23节 Python中使用sort/sorted排序与"富比较"方法的关系分析>中介绍了排序方法sort和函数sorted在没有提供key参数的情况下默认调用 ...

  7. 161018、springMVC中普通类获取注解service方法

    1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.sp ...

  8. 总结:js中4类修改样式的方法

    前言 最近在写一个扩展右键菜单的插件,既然是插件,想着一步到位,把相关的style样式设置都丢进js文件中,直接加载一个js文件便可以使用该插件,所以今天就研究了下js批量的插入样式的方法,即addS ...

  9. java中File类的常用所有方法及其应用

    创建:createNewFile()在指定位置创建一个空文件,成功就返回true,如果已存在就不创建,然后返回false.mkdir()  在指定位置创建一个单级文件夹.mkdirs()  在指定位置 ...

随机推荐

  1. 【JVM虚拟机】(8)--深入理解Class中--方法、属性表集合

    #[JVM虚拟机](8)--深入理解Class中--方法.属性表集合 之前有关class文件已经写了两篇博客: 1.[JVM虚拟机](5)---深入理解JVM-Class中常量池 2.[JVM虚拟机] ...

  2. 一个实时收集MySql变更记录的组件CanalSharp.AspNetCore

    一.关于CanalSharp CanalSharp 是阿里巴巴开源项目 Canal 的 .NET 客户端.为 .NET 开发者提供一个更友好的使用 Canal 的方式.Canal 是mysql数据库b ...

  3. TensorFlow从1到2(五)图片内容识别和自然语言语义识别

    Keras内置的预定义模型 上一节我们讲过了完整的保存模型及其训练完成的参数. Keras中使用这种方式,预置了多个著名的成熟神经网络模型.当然,这实际是Keras的功劳,并不适合算在TensorFl ...

  4. SpringBoot进阶教程(二十六)整合Redis之共享Session

    集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...

  5. 『性能』ServiceStack.Redis 和 StackExchange.Redis 性能比较

    背景 近来,需要用到 Redis 这类缓存技术 —— MongoDB 和 Redis 没有进行过比较. 我也懒得在这些细节上 纠结那么多 —— 按照网友给出的文章,听从网友建议,选择 Redis. R ...

  6. Docker最全教程之使用 Visual Studio Code玩转Docker(二十)

    前言 VS Code是一个年轻的编辑器,但是确实是非常犀利.通过本篇,老司机带你使用VS Code玩转Docker——相信阅读本篇之后,无论是初学者还是老手,都可以非常方便的玩转Docker了!所谓是 ...

  7. 30分钟玩转Net MVC 基于WebUploader的大文件分片上传、断网续传、秒传(文末附带demo下载)

    现在的项目开发基本上都用到了上传文件功能,或图片,或文档,或视频.我们常用的常规上传已经能够满足当前要求了, 然而有时会出现如下问题: 文件过大(比如1G以上),超出服务端的请求大小限制: 请求时间过 ...

  8. 排错-升级Exchange 2013 CU22后程序名称显示异常

    近期在按需更新Exchange 2013 CU22补丁以便解决Microsoft Exchange Server ADV190007 Guidance for "PrivExchange&q ...

  9. 解决Windows10运行VMware Workstation出现与Device Guard不兼容导致无法运行与创建虚拟机问题

    问题表现如下: 1.有Hyper-V功能未关闭的可能, 随便贴一个链接,网上一样的方法很多: https://jingyan.baidu.com/article/9f63fb916b50e1c8400 ...

  10. centos7.3 kubernetes/k8s 1.10 离线安装 --已验证

    本文介绍在centos7.3使用kubeadm快速离线安装kubernetes 1.10. 采用单master,单node(可以多node),占用资源较少,方便在笔记本或学习环境快速部署,不适用于生产 ...