简体   繁体   English

如何确定类型是否在继承层次结构中

[英]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.

相关问题 如何找到类型的接口继承层次结构? - How do I find the Interface Inheritance Hierarchy For a type? 如何在TPT继承层次结构中为子类型定义外键 - How to define a foreign key to a sub type in a TPT inheritance hierarchy 继承层次结构-如何拆分类 - Inheritance hierarchy - how to split class 如何为 inheritance 层次结构创建视图 - How to create a view for the inheritance hierarchy C# - 如何为多级继承层次结构指定泛型类型约束? - C# - How to specify generic type constraints for multiple level of inheritance hierarchy? 如何在双层次结构中正确表示继承? - How to properly represent inheritance in dual hierarchy? 如何将现有表映射到NHibernate中的继承层次结构? - How to map existing tables into a inheritance hierarchy in NHibernate? 使用Table Per Hierarchy继承在LINQ to Entities查询中转换为派生类型 - Casting to a derived type in a LINQ to Entities query with Table Per Hierarchy inheritance 是否可能对 inheritance 层次结构中的 class 的通用成员施加类型约束? - Is it possibly to impose type constraints on a generic member of a class in an inheritance hierarchy? 如何在Power Designer中确定继承的子级 - How to determine the child of an inheritance in Power Designer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM