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")
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.
“如果当前 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))
"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.
发布评论
评论(4)
您是否正在寻找类似
TypeOf
的内容? 这只适用于引用类型,不适用于 int/等。或者您想比较两个不同的变量实例? 也适用于引用类型:
如果您不使用两个对象,您也可以像这样使用
gettype()
:如果您想查看某物是否是另一种类型的子类(并且位于 . net 3.5):
但是如果你想在早期版本中这样做,你必须翻转它(看起来很奇怪)并使用:
所有这些都在 SnippetCompiler,所以如果你没有它,就去深度学习。
Are you looking for something like
TypeOf
? This only works with reference types, not int/etc.Or do you want to compare two different instances of variables? Also works for ref types:
You could also use
gettype()
like thus, if you aren't using two objects:If you want to see if something is a subclass of another type (and are in .net 3.5):
But if you want to do that in the earlier versions, you have to flip it (weird to look at) and use:
All of these compile in SnippetCompiler, so go DL if you don't have it.
不确定何时实现,但 VB 现在有 Type.IsInstanceOfType():
“如果当前 Type 位于 o 表示的对象的继承层次结构中,或者当前 Type 是 o 实现的接口,则返回 true...”
示例:
输出:
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:
Output:
Is int[] an instance of the Array class? True.
与您链接的问题等效的 VB 几乎相同:
The VB equivalent to your linked question is almost identical: