//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();
Se provo per deserializzare con il modo in cui sopra dà l'eccezione comedeserializzazione non funziona su MemoryStream
'Flusso binario '0' non contiene una BinaryHeader valida. Le possibili cause sono lo streaming non valido o il cambio di versione dell'oggetto tra la serializzazione e la deserializzazione. '
Dove sottostante Codice funziona
//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();
L'unica differenza è il primo approccio utilizza il metodo Read per compilare la matrice di byte in cui il secondo utilizza la Seek & ToArray() per compilare la matrice di byte. Qual è il motivo dell'eccezione.
Ah bene, stavo fissando quello per un po 'cercando di capirlo :) –
@dtb, Grazie. Ora ho capito il problema – Ragunath
@dtb grazie mille ho passato alcuni giorni su questo argomento T__T –