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

    <tfoot id='aOmVZ'></tfoot>

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

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

        如何二进制序列化器自定义类

        时间:2023-06-04

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

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

                  本文介绍了如何二进制序列化器自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有这个自定义类:

                  public class MyClass
                  { 
                      private byte byteValue;
                      private int intValue;
                      private MyClass myClass1= null;
                      private MyClass myClass2 = null;
                  }
                  

                  显然我也有构造函数和 get/set 方法.

                  obviously I also have constructor and get/set methods.

                  在我的主窗体中,我初始化了很多 MyClass 对象(请注意,在 MyClass 对象中我引用了其他 2 个 MyClass 对象).初始化后,我遍历第一个 MyClass 项,例如将其称为root".因此,例如,我会执行以下操作:

                  In my main form I initialize a lot of MyClass object (note that in MyClass object I have reference to other 2 MyClass objects). After initialization I iterate through a first MyClass item, call it for instance "root". So, for example I do something like:

                  MyClass myClassTest = root.getMyClass1();
                  MyClass myClassTest2 = myClassTest.getMyClass1();
                  

                  等等.

                  不,我想存储在二进制文件中,所有的 MyClass 对象都被实例化,以便在软件重启后再次获取它们.

                  No I want to store in a binary file, all the MyClass object instantiated, in order to get them again after software restart.

                  我完全不知道如何做到这一点,有人可以帮助我吗?谢谢.

                  I have totally no idea on how to do this, can someone please help me? Thanks.

                  推荐答案

                  首先在类声明前添加[Serializable]属性.有关属性的更多信息,请访问:https://msdn.microsoft.com/en-我们/图书馆/z0w1kczw.aspx

                  First add the attribute [Serializable] before the class declaration. More about the attributes go to: https://msdn.microsoft.com/en-us/library/z0w1kczw.aspx

                  [Serializable]
                  public class MyClass
                  { 
                      private byte byteValue;
                      private int intValue;
                      private MyClass myClass1= null;
                      private MyClass myClass2 = null;
                  }
                  

                  注意:所有类成员也必须是可序列化的.要将对象序列化为二进制,您可以使用以下代码示例:

                  Note: all the class members must be also serializable. For serializing the object to binary you can use the following code sample:

                  using (Stream stream = File.Open(serializationPath, FileMode.Create))
                          {
                              var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                              binaryFormatter.Serialize(stream, objectToSerialize);
                              stream.Close();
                          }
                  

                  对于从二进制反序列化:

                  And for the deserializing from binary:

                  using (Stream stream = File.Open(serializationFile, FileMode.Open))
                          {
                              var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                  
                              deserializedObject = (MyClass)binaryFormatter.Deserialize(stream);
                          }
                  

                  这篇关于如何二进制序列化器自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 C# 中将二进制数的字符串表示形式转换为 in 下一篇:如何将二进制转换为字符串?

                  相关文章

                  最新文章

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

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

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