<legend id='aAhqD'><style id='aAhqD'><dir id='aAhqD'><q id='aAhqD'></q></dir></style></legend>
      <tfoot id='aAhqD'></tfoot>
    1. <i id='aAhqD'><tr id='aAhqD'><dt id='aAhqD'><q id='aAhqD'><span id='aAhqD'><b id='aAhqD'><form id='aAhqD'><ins id='aAhqD'></ins><ul id='aAhqD'></ul><sub id='aAhqD'></sub></form><legend id='aAhqD'></legend><bdo id='aAhqD'><pre id='aAhqD'><center id='aAhqD'></center></pre></bdo></b><th id='aAhqD'></th></span></q></dt></tr></i><div id='aAhqD'><tfoot id='aAhqD'></tfoot><dl id='aAhqD'><fieldset id='aAhqD'></fieldset></dl></div>
        <bdo id='aAhqD'></bdo><ul id='aAhqD'></ul>

      1. <small id='aAhqD'></small><noframes id='aAhqD'>

        Dictionary&lt;Key, Value&gt; 上 .NET 二进制序列化

        时间:2023-06-04
      2. <i id='DMVuJ'><tr id='DMVuJ'><dt id='DMVuJ'><q id='DMVuJ'><span id='DMVuJ'><b id='DMVuJ'><form id='DMVuJ'><ins id='DMVuJ'></ins><ul id='DMVuJ'></ul><sub id='DMVuJ'></sub></form><legend id='DMVuJ'></legend><bdo id='DMVuJ'><pre id='DMVuJ'><center id='DMVuJ'></center></pre></bdo></b><th id='DMVuJ'></th></span></q></dt></tr></i><div id='DMVuJ'><tfoot id='DMVuJ'></tfoot><dl id='DMVuJ'><fieldset id='DMVuJ'></fieldset></dl></div>
        <tfoot id='DMVuJ'></tfoot>

          <bdo id='DMVuJ'></bdo><ul id='DMVuJ'></ul>
                <legend id='DMVuJ'><style id='DMVuJ'><dir id='DMVuJ'><q id='DMVuJ'></q></dir></style></legend>

                  <small id='DMVuJ'></small><noframes id='DMVuJ'>

                    <tbody id='DMVuJ'></tbody>
                1. 本文介绍了Dictionary&lt;Key, Value&gt; 上 .NET 二进制序列化的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在 .NET 的二进制序列化中遇到了一个奇怪的行为,至少在我的预期中.

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

                  Dictionary 的所有项目在 OnDeserialization 回调之后被添加到它们的父项.相比之下,List 则相反.这在现实世界的存储库代码中可能真的很烦人,例如当您需要向字典项添加一些委托时.请检查示例代码并观察断言.

                  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.

                  这是正常行为吗?

                  [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");
                      }
                  }
                  

                  推荐答案

                  我可以重现问题.环顾谷歌,发现:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94265 虽然我不确定这是完全相同的问题,但看起来非常相似.

                  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.

                  我认为添加此代码可能会解决问题?

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

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

                  没有时间进行详尽的测试,我想击败 Marc 得到答案 ;-)

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

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

                  上一篇:我如何编码一串 1 和 0 用于传输? 下一篇:以 C# BinaryReader.ReadString 的 7 位格式编码整数

                  相关文章

                  最新文章

                  <tfoot id='bB3SL'></tfoot>

                    <i id='bB3SL'><tr id='bB3SL'><dt id='bB3SL'><q id='bB3SL'><span id='bB3SL'><b id='bB3SL'><form id='bB3SL'><ins id='bB3SL'></ins><ul id='bB3SL'></ul><sub id='bB3SL'></sub></form><legend id='bB3SL'></legend><bdo id='bB3SL'><pre id='bB3SL'><center id='bB3SL'></center></pre></bdo></b><th id='bB3SL'></th></span></q></dt></tr></i><div id='bB3SL'><tfoot id='bB3SL'></tfoot><dl id='bB3SL'><fieldset id='bB3SL'></fieldset></dl></div>

                  1. <legend id='bB3SL'><style id='bB3SL'><dir id='bB3SL'><q id='bB3SL'></q></dir></style></legend>
                  2. <small id='bB3SL'></small><noframes id='bB3SL'>

                        <bdo id='bB3SL'></bdo><ul id='bB3SL'></ul>