NameValueCollection 反序列化期间幕后发生了什么?
.NET(反)序列化的幕后发生了什么,使其表现出以下场景中的行为方式? (这是一个测试应用程序,仅说明我的设置。)
我有一个带有 NameValueCollection 属性的类:
[Serializable]
public class MyClassWithNVC
{
public NameValueCollection NVC { get; set; }
}
它又包含在另一个类中:
[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);
}
}
我的测试程序如下:
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();
}
}
当我运行它时,我看到以下打印出来在控制台中:
NVC 在 Wrapper 的构造函数中为空。
NVC 在 Wrapper 的 ctor 之后不为 null,并且具有正确的值。
这是怎么回事? NameValueCollection 显然能够使用默认序列化来反序列化自身,但是 为什么反序列化会延迟并且不会在包装器构造函数中的 GetValue() 调用中发生?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我试图深入研究反序列化方法,但没有弄清楚它的具体实现方式。
看起来它会首先运行 ISerialized 代码实现。所有自定义代码完成后,它将反序列化所有自动序列化的对象(例如示例中的 MyClassWithNVC 类)。
如果您让
MyClassWithNVC
继承自ISerialized
,那么它将运行您的自定义反序列化代码,并且 MyClass.NVC 在 Wrapper 反序列化器方法中不为 NULL。因此,这不是
NameValueCollection
的特定行为,而是自动序列化的任何属性。I tried to dig a bit in the Deserialize method without finding out exact how it's implemented.
It seems it will first run the ISerializable code implementations. Once all custom code has finished it will deserialize all automatically serialized objects (like your MyClassWithNVC class in the sample).
If you let
MyClassWithNVC
inherit fromISerializable
then it runs your custom deserialization code, and MyClass.NVC is not NULL in the Wrapper deserializer method.So it's not specific behavior to
NameValueCollection
, but any property which is automatically serialized.