This relates to this question, but this time I'm trying to work out how to serialize the dictionary. I have a class that inherits from Dictionary that I need to be able to serialize.
The Serialization methods look like this, basically the values collection from the dictionary are added to the list, which is serialized.
[Serializable]
public class Collection: SortedDictionary<Key, Node>, ISerializable
{
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
List<Node> Nodes = new List<Node>();
// The "Values" mentioned here is the SortedDictionary's Values collection
Nodes.AddRange(Values);
info.AddValue("Nodes", Nodes, Nodes.GetType());
}
public Collection(SerializationInfo info, StreamingContext context)
: base(new Key.Comparer())
{
List<Node> SerValues = (List<Node>)info.GetValue("Nodes", typeof(List<Node>));
foreach (Node ThisNode in SerValues)
{
// This add method has been extended so that it automatically generates the key.
Add(ThisNode);
}
}
}
However when I the deserialize constructor is called, the list contains the right amount of values, just null values.
I suspect this is because the nodes haven't been deserialized yet (I know they can be serialized though), but then how can I fix this?