什么是Java的instanceof和isInstance()的VB等价物?
-
05-07-2019 - |
题
本着 c#问题..
在VB.NET中比较类类型的等价语句是什么?
解决方案
你在寻找像 TypeOf
这样的东西吗?这仅适用于引用类型,而不适用于int / etc.
If TypeOf "value" Is String Then
Console.WriteLine("'tis a string, m'lord!")
或者您想比较两个不同的变量实例?也适用于ref类型:
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 中编译,如果你没有,请去DL它
其他提示
TypeOf obj Is MyClass
与你的链接问题相当的VB几乎完全相同:
Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())
不隶属于 StackOverflow