键,值>在字典&LT .NET二进制序列化的奇怪行为; [英] Strange behaviour of .NET binary serialization on Dictionary<Key, Value>

查看:168
本文介绍了键,值>在字典&LT .NET二进制序列化的奇怪行为;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET中的二进制序列化遇到了,至少对我的期望,行为怪异。

词典所加载的所有项目也加入到他们的 OnDeserialization 回调后父。相比之下列表做的其他方式。这在现实世界中库code真的很烦人,例如,当你需要一些代表添加到字典中的项目。请检查例如code和观赏断言。

这是正常的行为呢?

  [Serializable接口]
公共类数据:IDeserializationCallback
{
    公开名单<字符串>清单{获得;组; }

    公共字典<字符串,字符串>字典{获得;组; }

    公共数据()
    {
        字典=新字典<字符串,字符串> {{你好,你好},{CU,CU}};
        名单=新的名单,其中,串> {你好,CU};
    }

    公共静态数据加载(字符串文件名)
    {
        使用(流流= File.OpenRead(文件名))
        {
            数据结果=(数据)新的BinaryFormatter()反序列化(流)。
            TestsLengthsOfDataStructures(结果);

            返回结果;
        }
    }

    公共无效保存(字符串文件名)
    {
        使用(流流= File.Create(文件名))
        {
            新的BinaryFormatter()序列化(流,这一点);
        }
    }

    公共无效OnDeserialization(对象发件人)
    {
        TestsLengthsOfDataStructures(本);
    }

    私有静态无效TestsLengthsOfDataStructures(数据资料)
    {
        Debug.Assert的(data.List.Count == 2,列表);
        Debug.Assert的(data.Dictionary.Count == 2,说文解字);
    }
}
 

解决方案

我可以重现该问题。有大约谷歌一看,发现这一点:<一href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94265">http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94265虽然我不知道它的确切同样的问题,似乎类似pretty的。

编辑:

我认为,这增加code可能已经解决了这一问题?

 公共无效OnDeserialization(对象发件人)
    {
            this.Dictionary.OnDeserialization(寄件人);
    }
 

没有时间去彻底测试,而我想要击败马克的答案; - )

I encountered a, at least to my expectations, strange behavior in the binary serialization of .NET.

All items of a Dictionary that are loaded are added to their parent AFTER the OnDeserialization callback. In contrast List does the other way. This can be really annoying in real world repository code, for example when you need to add some delegates to dictionary items. Please check the example code and watch the asserts.

Is it normal behaviour?

[Serializable]
public class Data : IDeserializationCallback
{
    public List<string> List { get; set; }

    public Dictionary<string, string> Dictionary { get; set; }

    public Data()
    {
        Dictionary = new Dictionary<string, string> { { "hello", "hello" }, { "CU", "CU" } };
        List = new List<string> { "hello", "CU" };
    }

    public static Data Load(string filename)
    {
        using (Stream stream = File.OpenRead(filename))
        {
            Data result = (Data)new BinaryFormatter().Deserialize(stream);
            TestsLengthsOfDataStructures(result);

            return result;
        }
    }

    public void Save(string fileName)
    {
        using (Stream stream = File.Create(fileName))
        {
            new BinaryFormatter().Serialize(stream, this);
        }
    }

    public void OnDeserialization(object sender)
    {
        TestsLengthsOfDataStructures(this);
    }

    private static void TestsLengthsOfDataStructures(Data data)
    {
        Debug.Assert(data.List.Count == 2, "List");
        Debug.Assert(data.Dictionary.Count == 2, "Dictionary");
    }
}

解决方案

I can reproduce the problem. Had a look around Google and found this: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94265 although I'm not sure it's the exact same problem, it seems pretty similar.

EDIT:

I think that adding this code may have fixed the problem?

    public void OnDeserialization(object sender)
    {
            this.Dictionary.OnDeserialization(sender);
    }

No time to exhaustively test, and I want to beat Marc to the answer ;-)

这篇关于键,值&gt;在字典&LT .NET二进制序列化的奇怪行为;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆