BinaryFormatter and Deserialization Complex objects

I suspect you just need to provide a deserialization constructor to C, since dictionary implements ISerializable:

protected C(SerializationInfo info, StreamingContext ctx) : base(info, ctx) {}

checked (passes):

 static void Main() {
     C c = new C();
     c.Add(123, new A { ID = 456});
     using(var ms = new MemoryStream()) {
         var ser = new BinaryFormatter();
         ser.Serialize(ms, c);
         ms.Position = 0;
         C clone = (C)ser.Deserialize(ms);
         Console.WriteLine(clone.Count); // writes 1
         Console.WriteLine(clone[123].ID); // writes 456
     }
 }

Leave a Comment