Java 的 instanceof 和 isInstance() 在 VB 中的等价物是什么?

发布于 2024-07-23 21:09:08 字数 170 浏览 8 评论 0原文

本着 c# 问题的精神。 .

VB.NET 中比较类类型的等效语句是什么?

In the spirit of the c# question..

What is the equivalent statements to compare class types in VB.NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿看小鸭子会跑 2024-07-30 21:09:08

您是否正在寻找类似 TypeOf 的内容? 这只适用于引用类型,不适用于 int/等。

If TypeOf "value" Is String Then
     Console.WriteLine("'tis a string, m'lord!")

或者您想比较两个不同的变量实例? 也适用于引用类型:

Dim one As Object = "not an object"
Dim two As Object = "also not an object, exactly"
Dim three as Object = 3D

If one.GetType.Equals( two.GetType ) Then WL("They are the same, man")
If one.GetType Is two.GetType then WL("Also the same")
If one.GetType IsNot three.GetType Then WL("but these aren't")

如果您不使用两个对象,您也可以像这样使用 gettype()

If three.GetType Is gettype(integer) then WL("is int")

如果您想查看某物是否是另一种类型的子类(并且位于 . net 3.5):

If three.GetType.IsSubclassOf(gettype(Object)) then WL("it is")

但是如果你想在早期版本中这样做,你必须翻转它(看起来很奇怪)并使用:

If gettype(Object).IsAssignableFrom(three.GetType) Then WL("it is")

所有这些都在 SnippetCompiler,所以如果你没有它,就去深度学习。

Are you looking for something like TypeOf? This only works with reference types, not int/etc.

If TypeOf "value" Is String Then
     Console.WriteLine("'tis a string, m'lord!")

Or do you want to compare two different instances of variables? Also works for ref types:

Dim one As Object = "not an object"
Dim two As Object = "also not an object, exactly"
Dim three as Object = 3D

If one.GetType.Equals( two.GetType ) Then WL("They are the same, man")
If one.GetType Is two.GetType then WL("Also the same")
If one.GetType IsNot three.GetType Then WL("but these aren't")

You could also use gettype() like thus, if you aren't using two objects:

If three.GetType Is gettype(integer) then WL("is int")

If you want to see if something is a subclass of another type (and are in .net 3.5):

If three.GetType.IsSubclassOf(gettype(Object)) then WL("it is")

But if you want to do that in the earlier versions, you have to flip it (weird to look at) and use:

If gettype(Object).IsAssignableFrom(three.GetType) Then WL("it is")

All of these compile in SnippetCompiler, so go DL if you don't have it.

浮世清欢 2024-07-30 21:09:08
TypeOf obj Is MyClass
TypeOf obj Is MyClass
苏佲洛 2024-07-30 21:09:08

不确定何时实现,但 VB 现在有 Type.IsInstanceOfType():

“如果当前 Type 位于 o 表示的对象的继承层次结构中,或者当前 Type 是 o 实现的接口,则返回 true...”

示例:

Dim arr(10) As Integer
If GetType(Array).IsInstanceOfType(arr) Then _
    Console.WriteLine("Is int[] an instance of the Array class? {0}.",
                       GetType(Array).IsInstanceOfType(arr))

输出: int[] 是 Array 类的实例吗? 确实如此。

Not sure when it was implemented but VB now has Type.IsInstanceOfType():

"Returns true if the current Type is in the inheritance hierarchy of the object represented by o, or if the current Type is an interface that o implements..."

Example:

Dim arr(10) As Integer
If GetType(Array).IsInstanceOfType(arr) Then _
    Console.WriteLine("Is int[] an instance of the Array class? {0}.",
                       GetType(Array).IsInstanceOfType(arr))

Output: Is int[] an instance of the Array class? True.

笑脸一如从前 2024-07-30 21:09:08

与您链接的问题等效的 VB 几乎相同:

Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())

The VB equivalent to your linked question is almost identical:

Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文