Can not deserialize following object graph. That Exception occurs when deserialize method called on BinaryFormmater: System.Runtime.Serialization.SerializationException :
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
}
}
Your serialization will succeed when you implement class C as following:
[Serializable]
public class C : IDictionary<int,A>
{
private Dictionary<int,A> _inner = new Dictionary<int,A>;
// implement interface ...
}
The problem is the serialization of the Dictionary derived class.