Hey
Your call to PrintMe in the constructor for SerializeMe does not work because your object is created inside out so the NameValueCollection has not yet been fully constructed when you call PrintMe
I have tried to find a solution for this. I tried letting SerializeMe implement IDeserializationCallback and the call PrintMe in the implementation of OnDeserialization. This does not seem to work. However, NameValueCollection also implements IDeserializationCallback so I tried creating a new class inheriting NameValueCollection and then overriding NameValueCollection.OnDeserialization. This ALMOST works since I am able to get the correct item count and I am also able to extract the keys. However, for some strange reason, if I try to extract the values of the NameValueCollection, I get an exception
Instead of inheriting from NameValueCollection I then tried inheriting from Hashtable. In the override of OnDeserialization I can now extract and print the values of the hashtable. So Hashtable and NameValueCollection do not behave the same way
Here is my code
[Serializable
public class NameValueCollectionEx : System.Collections.Hashtabl
public NameValueCollectionEx(
: base(
protected NameValueCollectionEx(SerializationInfo info, StreamingContext context)
: base(info, context
public override void OnDeserialization(object sender
base.OnDeserialization (sender)
Console.WriteLine("Items: " + this.Count)
foreach (string key in this.Keys)
Console.WriteLine("Key: " + key + "->" + this[key].ToString())
[Serializable
public class SerializeMe : ISerializable, IDeserializationCallbac
private NameValueCollectionEx values
public SerializeMe()
values = new NameValueCollectionEx()
values.Add("string1", "hi")
values.Add("string2", "there")
protected SerializeMe(SerializationInfo info, StreamingContext context)
values = (NameValueCollectionEx) info.GetValue("values", typeof(NameValueCollectionEx))
// The rest of your code goes here..
Regards, Jakob.