以下 VB.Net 代码在运行时失败并出现InvalidCastException
错误。如果我将变量的类型a1
从更改Object
为class A
则程序执行正常。
我想在不更改Test()
下面示例代码中的函数的情况下解决此问题。如果需要,我可以轻松更改Class A
or的定义。Class B
Public Class A
Public Shared Widening Operator CType(ByVal d As A) As B
Return New B
End Operator
End Class
Public Class B
End Class
Module X
Public Sub Test()
Dim a1 As Object = New A
Dim b1 As B
b1 = a1 ' Fails with System.InvalidCastException
End Sub
End Module