C# - 类与类之间的继承关系判断

03-03 阅读 0评论

Type.IsSubclassof(Type type)

作用:用来确定 一个类是否派生自另一个类/ValueType/Enum/委托

C# - 类与类之间的继承关系判断,C# - 类与类之间的继承关系判断,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,接口,关系,作用,第1张
(图片来源网络,侵删)

不能用于确定:接口是派生自另一个接口,还是类实现接口

class A{}
class B : A{}
A a;
B b;
var boo = b.GetType().IsSubclassOf(typeof(A)) // ture

Type.IsAssignableFrom(Type type)

作用:用于确定 接口是派生自另一个接口,还是类实现接口

class A{}
class B : A{}
A a;
B b;
var boo = a.GetType().IsAssignable(typeof(b)) // ture

自定义关系判断

		/// 
		/// 兼顾类与类 类与接口 接口与接口
		/// 
		/// 目标子类型
		/// 目标基类型
		/// 
		public static bool IsAssignableType(Type targetType, Type targetBaseType)
		{
			return targetType?.IsSubclassOf(targetBaseType) != false || targetBaseType?.IsAssignableFrom(targetType) != false;
		}
interface IData { }
class A : IData { }
class B : A { }		
var boo = FEditorUtility.IsAssignableType(typeof(B), typeof(A));
Debug.Log(boo);//true
boo = typeof(B).IsSubclassOf(typeof(A));
Debug.Log(boo);//true
boo = typeof(B).IsSubclassOf(typeof(IData));
Debug.Log(boo);//false
boo = typeof(A).IsAssignableFrom(typeof(B));
Debug.Log(boo);//true
boo = typeof(IData).IsAssignableFrom(typeof(B));
Debug.Log(boo);//true
C# - 类与类之间的继承关系判断,C# - 类与类之间的继承关系判断,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,接口,关系,作用,第2张
(图片来源网络,侵删)
C# - 类与类之间的继承关系判断,C# - 类与类之间的继承关系判断,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,接口,关系,作用,第3张
(图片来源网络,侵删)

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]