NameValueCollection 反序列化期间幕后发生了什么?

发布于 2024-08-23 01:50:48 字数 2011 浏览 2 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-08-30 01:50:48

我试图深入研究反序列化方法,但没有弄清楚它的具体实现方式。

看起来它会首先运行 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 from ISerializable 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文