如何检查变量的最终类型?
我有一个来自第三方库的 BaseClass、DerivedClass1 和 DerivedClass2。 DerivedClass1 和 DerivedClass2 都继承自 BaseClass。
有一个ContainerClass,来自同一个库,有一个成员变量ActiveItem,它可以是DerivedClass1或DerivedClass2,所以它被声明为BaseClass。
我想知道 ActiveItem 是否属于 DerivedClass1,因为它可以在运行时更改,恕不另行通知。
如果这样做
Dim isDerivedClass1 as boolean = TypeOf(oject.ActiveItem) Is DerivedClass1
,则会出现编译时错误,告诉我 ActiveItem 永远不能是 DerivedClass1 类型。
我已经尝试了 GetType 和 TypeOf 的几种组合,但似乎无法检查这一点。我还尝试声明一个辅助 DerivedClass1 变量并比较它们的类型,但也没有任何运气。
有什么解决方法吗? 我想我可以用反射来做到这一点,但看起来确实有点矫枉过正。
编辑: 以下代码在 vs2005 SP1 中无法编译。
Public Class Base
Public x As Integer
End Class
Public Class Derived1
Inherits Base
Public y As Integer
End Class
Public Class Derived2
Inherits Base
Public z As Integer
End Class
Public Class Unrelated
Public var As Base
End Class
Public Class Form1
Public Sub Test(ByVal obj As Unrelated)
Dim tst As Boolean
tst = TypeOf obj Is Derived1
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim obj As New Unrelated
obj.var = New Derived1
Test(obj)
End Sub
End Class
编辑: 看来原来的问题是我这边的错。我正在检查错误的类型(那些愚蠢的第三方库......) 但是,我仍在尝试查找上面代码中的错误。
编辑: 又是我的错。我正在对照基本检查不相关类型。
I have a BaseClass, a DerivedClass1 and a DerivedClass2 from a third party library. DerivedClass1 and DerivedClass2 both inherit from BaseClass.
There's a ContainerClass, from the same library, with a member variable ActiveItem, which can be of DerivedClass1 or DerivedClass2, so it is declared as BaseClass.
I want to know if ActiveItem is of DerivedClass1, as it can change in runtime without notice.
If I do
Dim isDerivedClass1 as boolean = TypeOf(oject.ActiveItem) Is DerivedClass1
then I get a compile time error, telling me that ActiveItem can never be of DerivedClass1 type.
I have tried several combinations of GetType and TypeOf but it doesn't seem possible to check this. I have also tried to declare an auxiliary DerivedClass1 variable and comparing their types, but haven't got any luck either.
Is there any workaround?
I guess I could do it with Reflection, but seems really overkill.
Edit:
The following code doesn't compile in vs2005 SP1.
Public Class Base
Public x As Integer
End Class
Public Class Derived1
Inherits Base
Public y As Integer
End Class
Public Class Derived2
Inherits Base
Public z As Integer
End Class
Public Class Unrelated
Public var As Base
End Class
Public Class Form1
Public Sub Test(ByVal obj As Unrelated)
Dim tst As Boolean
tst = TypeOf obj Is Derived1
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim obj As New Unrelated
obj.var = New Derived1
Test(obj)
End Sub
End Class
Edit:
It seems that the original problem was a fault in my side. I was checking against the wrong type (those silly third part libraries...)
However, I'm still trying to find the error in the code above.
Edit:
Again, my fault. I'm checking the Unrelated type against Base.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码似乎几乎完全正确。
我已经这样做了,效果很好:
我错过了什么吗?
编辑:我认为您刚刚错过了编辑代码中的
var
属性。You code seems to be almost exactly right.
I've done this, which works fine:
Have I missed something?
EDIT: I think you just missed the
var
property in your edited code.您必须相信编译器这一点,它确信 DerivedClass1 确实不继承 BaseClass。这并没有错。这要么是因为它在 DerivedClass1 声明中没有看到 Inherits 子句,要么是因为它从另一个程序集中选择了 BaseClass 定义。
要解决前一个问题,您别无选择,只能将 ActiveItem 声明为 Object 或找到这些类共有的另一种类型。使用对象浏览器。要解决后一个问题,您必须更改 Imports 指令或拼出 BaseClass 类型的全名(包括命名空间)。
You'll have to trust the compiler on this, it is convinced that DerivedClass1 does not inherit BaseClass. It doesn't get that wrong. That's either because it didn't see the Inherits clause in the DerivedClass1 declaration or because it picked a BaseClass definition from another assembly.
To fix the former problem, you have no alternative but to declare the ActiveItem as Object or to find another type that these classes have in common. Use the Object Browser. To fix the latter problem you'll have to change the Imports directive or spell out the full name of the BaseClass type (including namespace).
一个简单的可能性是使用
TryCast
:A simple possibility could be to use
TryCast
: