21
Dato il seguente esempio:Il costruttore non viene trovato durante la deserializzazione?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace SerializationTest
{
[Serializable]
class Foo : Dictionary<int, string>
{
}
class Program
{
static void Main(string[] args)
{
Foo foo = new Foo();
foo[1] = "Left";
foo[2] = "Right";
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, foo);
stream.Seek(0, SeekOrigin.Begin);
formatter.Deserialize(stream);
}
}
}
Nell'ultima riga, un SerializationException è gettato in quanto il formattatore non riesce a trovare il costruttore a Foo. Perché?
E tutto perché il dizionario implementa ISerializable, ovviamente ... –
Nota per le classi SerializationInfo e StreamingContext, sono nello spazio dei nomi ** System.Runtime.Serialization **. Il percorso completo è ** System.Runtime.Serialization.SerializationInfo **. – Wappenull