【发布时间】:2016-03-09 22:33:54
【问题描述】:
我有一个字节数组,例如:
Dim byteArray(10) as Byte
byteArray(0) = 1
byteArray(1) = 2
byteArray(2) = 3
...
byteArray(9) = 10
我正在尝试将其转换为对象但没有成功。我在这里阅读了很多关于如何做到这一点的帖子,所以我有以下功能:
Public Shared Function ByteArrayToObject(ByVal arrBytes As Byte()) As Object
Using ms As New MemoryStream()
Dim binForm As New BinaryFormatter()
ms.Write(arrBytes, 0, arrBytes.Length)
ms.Seek(0, SeekOrigin.Begin)
Dim obj As Object = DirectCast(binForm.Deserialize(ms), Object)
Return obj
End Using
End Function
但在执行 DirectCast 时,我收到一个异常,或多或少(从西班牙语翻译):
"SerializationException was unhandled: End of sequence reached before terminating analysis".
知道为什么会这样吗?
【问题讨论】:
-
字节序列
1 2 3 4 5 6 7 8 9 10听起来不像Object的序列化实例。你到底想在这里做什么? -
你试过
CType(byteArray, Object)吗? -
为什么不创建一个可序列化的结构或类?
标签: vb.net serialization memorystream binaryformatter directcast