[英]How to determine if a type is in the inheritance hierarchy
I need to determine if an object has a specific type in it's inheritance hierarchy, however I can't find a good way of doing it.我需要确定一个对象在它的继承层次结构中是否具有特定类型,但是我找不到这样做的好方法。
An very basic example version of my classes are:我的课程的一个非常基本的示例版本是:
Public Class Domain
End Class
Public Class DerivedOne
Inherits Domain
End Class
Public Class DerivedTwo
Inherits DerivedOne
End Class
Public Class DerivedThree
Inherits Domain
End Class
The following does work, however it isn't very elegant in my opinion.以下确实有效,但在我看来它不是很优雅。 Also the more levels of inheritance that get created, the more checks need to be done and it would be easy to forget this piece of code needs to be updated.此外,创建的继承级别越多,需要进行的检查就越多,很容易忘记这段代码需要更新。
If GetType(T) Is GetType(Domain) OrElse _
GetType(T).BaseType Is GetType(Domain) OrElse _
GetType(T).BaseType.BaseType Is GetType(Domain) Then
End If
Is there a way of getting 'Is type of Domain anywhere in T's inheritance hierarchy'?有没有办法获得“域的类型是否在 T 的继承层次结构中的任何地方”?
(Answers welcome in C# or VB.NET) (欢迎在 C# 或 VB.NET 中回答)
UPDATE更新
One bit of vital information I missed out due to my own idiocy!由于自己的愚蠢,我错过了一点重要信息!
T is a Type object (from the class' generic type) T 是一个 Type 对象(来自类的泛型类型)
You can use the Type.IsAssignableFrom
method.您可以使用Type.IsAssignableFrom
方法。
In VB:在VB中:
If GetType(Domain).IsAssignableFrom(GetType(DerivedThree)) Then
In C#:在 C# 中:
if (typeof(Domain).IsAssignableFrom(typeof(DerivedThree)))
Why is nobody mentioning Type.IsSubclassOf(Type)
?为什么没有人提到Type.IsSubclassOf(Type)
?
https://docs.microsoft.com/en-us/dotnet/api/system.type.issubclassof?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.type.issubclassof?view=netframework-4.7.2
Careful, it returns false if called for two equal types ;)
小心,如果调用两个相等的类型,它会返回 false ;)
I need to determine if an object has a specific type in it's inheritance hierarchy, however I can't find a good way of doing it.我需要确定对象的继承层次结构中是否具有特定类型,但是我找不到一种好的方法。
An very basic example version of my classes are:我的课程的一个非常基本的示例版本是:
Public Class Domain
End Class
Public Class DerivedOne
Inherits Domain
End Class
Public Class DerivedTwo
Inherits DerivedOne
End Class
Public Class DerivedThree
Inherits Domain
End Class
The following does work, however it isn't very elegant in my opinion.下面的方法确实有效,但是我认为它不是很优雅。 Also the more levels of inheritance that get created, the more checks need to be done and it would be easy to forget this piece of code needs to be updated.同样,创建的继承级别越高,需要执行的检查就越多,并且很容易忘记需要更新的这段代码。
If GetType(T) Is GetType(Domain) OrElse _
GetType(T).BaseType Is GetType(Domain) OrElse _
GetType(T).BaseType.BaseType Is GetType(Domain) Then
End If
Is there a way of getting 'Is type of Domain anywhere in T's inheritance hierarchy'?有没有办法获取“ T的继承层次结构中的任何地方的域类型”?
(Answers welcome in C# or VB.NET) (欢迎在C#或VB.NET中回答)
UPDATE更新
One bit of vital information I missed out due to my own idiocy!由于自己的愚蠢,我错过了一些重要信息!
T is a Type object (from the class' generic type) T是一个Type对象(来自类的通用类型)
How about is operator ? 运营商怎么样?
if(obj is Domain)
{
//
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.