【发布时间】:2010-12-22 18:43:45
【问题描述】:
作为对象的公共 MyObj
Public Function Test(t as Type) as Boolean
Return TypeOf MyObj is t ' does not work
End Function
谢谢。
* 编辑
================================================ ======================
为了清楚起见,我将使用一个完整的示例,最初的想法稍作修改。
我使用一个接口,并且想知道传入参数的对象(实现 I)是否与内部字段类型相同(I; A:I; AA:A; B:I强>)。
首先,(a)我需要准确的类标识(仅A=A, B=B, AA=AA) 而且我还希望 (b) 具有“继承等价性”(A=A and A=AA but AB)
Option Strict On
Option Explicit On
Public Class Form1
' object to test
Private objI As I
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim aObj As New A
Dim aaObj As New AA
Dim bObj As New B
'assing the object to test - A
Me.objI = New A
'test it
MessageBox.Show(SameTypeAs(aObj).ToString()) ' need True here
MessageBox.Show(SameTypeAs(bObj).ToString()) ' need False here
MessageBox.Show(SameTypeAs(aaObj).ToString()) ' need a)Flase and b)True here
End Sub
Function SameTypeAs(ByVal iObj As I) As Boolean
' here is the "problem":
' how to detect the same (sub)types of I?
Return Me.objI Is iObj
End Function
' interface..........
Interface I
End Interface
' class A..........
Class A
Implements I
End Class
' class B..........
Class B
Implements I
End Class
' class AA..........
Class AA
Inherits A
End Class
End Class
【问题讨论】:
-
还有什么问题? “不起作用”不完全是描述性的