【发布时间】:2013-10-14 14:38:20
【问题描述】:
我正在 VB 中编写以下代码:
Public Shared Function LoadFromSession(Of T)(sessionKey As String) As T
Try
' Note: SBSSession is simply a reference to HttpContext.Current.Session
Dim returnValue As T = DirectCast(SBSSession(sessionKey), T)
Return returnValue
Catch ex As NullReferenceException
' If this gets thrown, then the object was not found in session. Return default value ("Nothing") instead.
Dim returnValue As T = Nothing
Return returnValue
Catch ex As InvalidCastException
' Instead of throwing this exception, I would like to filter it and only
' throw it if it is a type-narrowing cast
Throw
End Try
End Function
我想做的是为任何缩小转换抛出异常。例如,如果我将 5.5 之类的十进制数保存到会话中,然后尝试将其检索为整数,那么我想抛出 InvalidCastException。 DirectCast 可以做到这一点。
但是,我希望允许扩大转换(例如,将像 5 这样的整数保存到会话中,然后将其作为小数检索)。 DirectCast 不允许这样做,但 CType 允许。不幸的是,CType 还允许缩小转换范围,这意味着在第一个示例中,它将返回值 6。
有没有办法可以实现所需的行为?也许通过使用 VB 的Catch...When 过滤异常?
【问题讨论】:
-
嗯,不,DirectCast 只允许将值拆箱为与装箱值类型完全相同的类型。这有太多问题,例如从整数到单数的转换不能可靠地工作。在值 16777217 上尝试一下。 Double to Decimal 也不起作用,范围不够。对此没有很好的解决方案,最好不要这样做。
-
@HansPassant 我主要关心 Integer to Decimal,但我想我最好还是在 DirectCast 想抛出 InvalidCastException 时抛出它。
-
您也许可以使用 GetType 方法来查找涉及的类型并过滤 InvalidCastException Catch 块中的缩小转换。
标签: vb.net casting type-conversion