Ciao,
devo serializzare una hashtable e poi deserializzarla, quando faccio la serializzazione va tutto bene ma se controllo la matrice di bytes che mi restituisce sembra essere vuota, infatti se deserializzo mi dice: "Tentativo di deserializzare un flusso vuoto.".
questi sono i metodi:
private static byte[] serialize(Hashtable extra) {
byte[] buffer = null;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
try {
formatter.Serialize(ms, extra);
if (ms != null && ms.Length > 0) {
buffer = new byte[ms.Length];
ms.Write(buffer, 0, (int)ms.Length);
}
} catch (SerializationException ex) {
throw new Exception("Errore nella serializzazione dei dati 'extra'.", ex);
} catch (Exception ex) {
throw new Exception(ex.Message, ex);
}
return buffer;
}
private static Hashtable deserialize(byte[] buffer) {
Hashtable obj = null;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
try {
if (buffer != null && buffer.Length > 0) {
//ms = new MemoryStream(buffer);
ms.Read(buffer, 0, buffer.Length - 1);
//ms.Seek(0, SeekOrigin.Begin);
obj = (Hashtable)formatter.Deserialize(ms);
}
} catch (SerializationException ex) {
throw new Exception("Errore nella serializzazione dei dati 'extra': " + ex.Message, ex);
} catch (Exception ex) {
throw new Exception(ex.Message, ex);
}
return obj;
}