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() 在指定位置 ...
随机推荐
- “==”、“equals()”、“hashcode()”之间的秘密
前言 万丈高楼平地起,今天的聊点基础而又经常让人忽视的话题,比如“==”与“equals()”区别?为何当我们重写完"equals()"后也要有必要去重写"hashcod ...
- 【干货分享】可能是东半球最全的.NET Core跨平台微服务学习资源
如果你发现还有西半球的资源,烦请相告,不胜感谢! 一..NET Core基础 微软英文官网 .NET Core 微软中文官网 GitHub 用ASP.NET内核和Azure构建现代Web应用程序 博客 ...
- .netcore2.1使用swagger显示接口说明文档
项目之前开发完接口后,我们还需要写接口说明文档,现在有了swagger方便了很多,可以网页版直接测试,当然了也减少了我们的工作量. 使用swagger生成接口说明文档,大致需要2个步骤 1.从“管理 ...
- vue 中使用promise
init1(){return new Promise((resolve, reject) => { let data={ dateStr:this.time }; api.get('url', ...
- 前端css
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...
- win10环境下如何运行debug
在学习汇编的时候,会需要用到debug调试程序,但是现在win10默认已经移除了这个插件,我们需要手动安装,下面就告诉大家如何在win10环境下安装debug. 1:准备工具 1.1 DOSBox 1 ...
- 基于OpenStreetMap计算驾车距离(Java)
最近公司有个项目需要计算6000个点之间的驾车距离,第一时间想到的是利用Google的Distance Matrix API,但是免费Key每天只能计算2500个元素(元素 = 起点数量 * 终点数量 ...
- Linux集群时间同步方法
方法1.ntp 平滑同步时间 (一)确认ntp的安装 1)确认是否已安装ntp [命令] rpm –qa | grep ntp 若只有ntpdate而未见ntp,则需删除原有ntpdate.如: n ...
- python 基础语法梳理
最近涉及到python的东西比较多,抽一点时间把基础语法规整下. 1.面向对象 #coding=utf-8 def _class_test_01(): s = squire(3,4) print(&q ...
- Cocos Creator 资源加载流程剖析【二】——Download部分
Download流程的处理由Downloader这个pipe负责(downloader.js),Downloader提供了各种资源的"下载"方式--即如何获取文件内容,有从网络获取 ...