What's going on behind the scenes in .NET (de)serialization that makes it behave the way it does in the following scenario? (This is a test app that just illustrates my setup.)
I have a class with a NameValueCollection property:
[Serializable]
public class MyClassWithNVC
{
public NameValueCollection NVC { get; set; }
}
It, in turn, is contained in another class:
[Serializable]
class Wrapper : ISerializable
{
public MyClassWithNVC MyClass { get; private set; }
public Wrapper()
{
MyClass = new MyClassWithNVC
{
NVC = new NameValueCollection
{
{"TestKey", "TestValue"}
}
};
}
public Wrapper(SerializationInfo info, StreamingContext context)
{
MyClass = info.GetValue("MyClass", typeof(MyClassWithNVC)) as MyClassWithNVC;
if(MyClass.NVC == null)
{
Console.WriteLine("NVC is null inside Wrapper's ctor.");
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("MyClass", MyClass);
}
}
My test program is below:
class Program
{
static void Main(string[] args)
{
using(MemoryStream ms = new MemoryStream())
{
Wrapper wrapper = new Wrapper();
new BinaryFormatter().Serialize(ms, wrapper);
ms.Seek(0, SeekOrigin.Begin);
Wrapper loaded = new BinaryFormatter().Deserialize(ms) as Wrapper;
if(loaded.MyClass.NVC.Count == 1 && loaded.MyClass.NVC[0] == "TestValue")
{
Console.WriteLine("NVC is not null after Wrapper's ctor and has the correct value.");
}
}
Console.ReadKey();
}
}
When I run it, I see the following printed out in the console:
NVC is null inside Wrapper's ctor.
NVC is not null after Wrapper's ctor and has the correct value.
What's going on here? The NameValueCollection is obviously capable of deserializing itself with the default serialization, but why is that deserialization delayed and not happening at the GetValue() call in the Wrapper's constructor?