我试图将字节数组转换为对象。为了消除任何可能的问题,我创建了一个简单的窗体,它简单地调用了原始代码中打破的函数,并得到相同的错误。关于发生什么事情的任何想法?序列化异常:分析完成之前遇到的流结束 - C#
private void button1_Click(object sender, EventArgs e)
{
byte[] myArray = new byte[] {1, 2, 3, 4, 5, 6, 7};
object myObject = ByteArrayToObject(myArray);
if(myObject != null)
{
button1.Text = "good";
}
}
private object ByteArrayToObject(byte[] arrBytes)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binForm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MemoryStream memStream = new MemoryStream(arrBytes);
memStream.Position = 0;
return binForm.Deserialize(memStream);
}
什么让你觉得那个特定的字节数组产生一个有效的对象? –
什么会限定或取消一个字节数组生成一个有效的对象的资格? –
二进制序列化不仅仅是序列化字节。它是*类型安全的*,它将元数据添加到描述对象的流中。 –