//Serialize the Object
MemoryStream ms = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms , ObjectToSerialize);
byte[] arrbyte = new byte[ms .Length];
ms.Read(arrbyte , 0, (int)ms .Length);
ms.Close();
//Deserialize the Object
Stream s = new MemoryStream(arrbyte);
s.Position = 0;
Object obj = formatter.Deserialize(s);//Throws an Exception
s.Close();
Nếu tôi cố gắng Deserialize với cách trên nó mang lại cho những ngoại lệ nhưDeserialization không làm việc trên MemoryStream
'dòng nhị phân '0' không chứa một BinaryHeader hợp lệ. Nguyên nhân có thể là luồng không hợp lệ hoặc thay đổi phiên bản đối tượng giữa tuần tự hóa và deserialization. '
đâu dưới đây đang làm việc
//Serialize the Object
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, ObjectToSerialize);
ms.Seek(0, SeekOrigin.Begin);
byte[] arrbyte = ms.ToArray();
//Deserialize the Object
Stream s= new MemoryStream(byt);
stream1.Position = 0;
Object obj = formatter.Deserialize(s);
stream1.Close();
Sự khác biệt duy nhất là cách tiếp cận đầu tiên sử dụng phương pháp đọc để cư trú trong mảng byte nơi như một thứ hai sử dụng tìm kiếm & ToArray() để cư trú trong mảng byte. Lý do ngoại lệ là gì.
Ah tốt đẹp, tôi đã nhìn chằm chằm vào đó trong một thời gian cố gắng tìm ra nó :) –
@ dtb, Cảm ơn. Bây giờ tôi hiểu được vấn đề – Ragunath
@ dtb cảm ơn bạn rất nhiều tôi đã dành vài ngày về vấn đề này T__T –