我想将null值转换为Nullable(Of)-Type。我可以使用CType()进行转换,但不能使用System.Convert.ChangeType()进行转换。
有没有办法这样做?为什么会抛出异常?
Dim b as Boolean? = CType(Nothing, Boolean?) 'ok
System.Convert.ChangeType(Nothing, GetType(Boolean?)) 'Throws System.InvalidCastException
答案 0 :(得分:2)
有办法吗?
let obsList = [ for x in 0..80 -> (Observable.map (fun _-> x) ((Array.get(gameButtons)(x)).Click)) ]
let rec obsMerger obsList =
match obsList with
| x1::[] -> x1
| x1::xs -> Observable.merge x1 (obsMerger xs)
| [] -> failwith "No Observables"
为什么会引发异常?
由于Dim valueNothing = ConvertHelper.SafeChangeType(Of Boolean)(Nothing)
Dim valueTrue = ConvertHelper.SafeChangeType(Of Boolean)(True)
Dim valueFalse = ConvertHelper.SafeChangeType(Of Boolean)(False)
' ...
Class ConvertHelper
Shared Function SafeChangeType(Of T As Structure)(ByVal value As Object) As T?
Return If(value Is Nothing, Nothing, DirectCast(Convert.ChangeType(value, GetType(T)), T?))
End Function
End Class
方法Secure Cookie
:
Convert.ChangeType