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

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

问题描述



我遇到一个,至少对我的期望,在.NET的二进制序列化中有奇怪的行为。 / code>被加载到他们的父母之后的 OnDeserialization 回调。相反,列表做另一种方法。这在真实的存储库代码中可能真的很讨厌,例如当你需要添加一些委托到字典项目时。请检查示例代码并查看断言。



是否正常行为?

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

public Dictionary< string,string>字典{get;组; }

public Data()
{
字典=新字典< string,string> {{hello,hello},{CU,CU}};
List = new List< string> {hello,CU};
}

public static Data Load(string filename)
{
using(Stream stream = File.OpenRead(filename))
{
数据结果=(数据)new BinaryFormatter()。反序列化(stream);
TestsLengthsOfDataStructures(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(数据数据)
{
Debug.Assert(data.List.Count == 2,List);
Debug.Assert(data.Dictionary.Count == 2,Dictionary);
}
}


解决方案

可以重现问题。看看Google,发现这一点: http://connect.microsoft。 com / VisualStudio / feedback / ViewFeedback.aspx?FeedbackID = 94265 虽然我不知道这是完全一样的问题,看起来很相似。



编辑:



我认为添加此代码可能已经解决了问题?

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

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


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 ;-)

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

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