我有一个字节数组,例如:
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".
知道为什么会这样吗?