無法讀取資料流末端之外
的錯誤。
MemoryStream stream = new MemoryStream(); var formater = new BinaryFormatter(); FileExpressPack pack = new FileExpressPack { Name = fileName, Data = byteData }; formater.Serialize(stream, pack); var outPak = formater.Deserialize(stream); // throw exception stream.Close();
從訊息字面上看不出原因,基本上是序列化完,串流的指標在最後一 index 往外數1,可是反序列化時需要從 0 開始,所以要把指標設回來。
MemoryStream stream = new MemoryStream(); var formater = new BinaryFormatter(); FileExpressPack pack = new FileExpressPack { Name = fileName, Data = byteData }; formater.Serialize(stream, pack); stream.Seek(0, SeekOrigin.Begin); // add this var outPak = formater.Deserialize(stream); stream.Close();
ref: SOF
沒有留言:
張貼留言