我試圖將字節數組轉換爲對象。爲了消除任何可能的問題,我創建了一個簡單的窗體,它簡單地調用了原始代碼中打破的函數,並得到相同的錯誤。關於發生什麼事情的任何想法?序列化異常:分析完成之前遇到的流結束 - 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);
}
什麼讓你覺得那個特定的字節數組產生一個有效的對象? –
什麼會限定或取消一個字節數組生成一個有效的對象的資格? –
二進制序列化不僅僅是序列化字節。它是*類型安全的*,它將元數據添加到描述對象的流中。 –