简体   繁体   English

C#中对象序列化异常的ArrayList

[英]ArrayList of objects serialization exception in c#

I have an ArrayList which consist of lots of object created by me. 我有一个ArrayList,其中包含许多由我创建的对象。 I am trying to keep it. 我正在努力保留它。 As far as I look, the best solution for that is to use binary formatter. 就我而言,最好的解决方案是使用二进制格式化程序。 But something is wrong with my code. 但是我的代码出了点问题。 It doesn't work either writing or reading. 它既不能写作也不能阅读。 Here is my code; 这是我的代码;

namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
      private ArrayList allList = new ArrayList();    
      private FileStream fileStream;
      private MemoryStream memoryStream;

      public Form1()
      {
          InitializeComponent();
          fileStream = new FileStream("pcdata.dat",FileMode.OpenOrCreate,FileAccess.ReadWrite);
          memoryStream = new MemoryStream();
      }
      public void acceptDevice(Device receivedDevice)
      {
          //Somehow I call this method
          allList.Add(receivedDevice);
          saveDeviceDataToFileStream();
      }
      private void saveDeviceDataToFileStream()
      {
          SerializeToStream(allList).WriteTo(fileStream);
      }
      private void loadDeviceDataFromFileStream()
      {
          fileStream.CopyTo(memoryStream);
          allList = (ArrayList)DeserializeFromStream(memoryStream);
      }
      public static MemoryStream SerializeToStream(object o)
      {
          MemoryStream stream = new MemoryStream();
          IFormatter formatter = new BinaryFormatter();
          formatter.Serialize(stream, o);
          return stream;
      }
      public static object DeserializeFromStream(MemoryStream stream)
      {
          IFormatter formatter = new BinaryFormatter();
          stream.Seek(0, SeekOrigin.Begin);
          if (null == stream)
          {
             return null;
          }
          object o = formatter.Deserialize(stream);
          return o;
    }
  }
}

This is my device class: 这是我的设备类:

namespace WindowsFormsApplication1
{
    [Serializable]
    public class Device
    {
        public MyPanel panel;           //panel class that I created
        public String id;
        public int deviceNumber;

        public Device(String id, int deviceNumber)
        {
            panel = new MyPanel();
            this.id = id;
            this.deviceNumber = deviceNumber;
        }
    }
}

And this is myPanel class: 这是myPanel类:

namespace WindowsFormsApplication1
{
    public class MyPanel : TableLayoutPanel
    {
        public Panel panel1 = new Panel();
        public Panel panel2 = new Panel();
        public Panel panel3 = new Panel();
        public Panel panel4 = new Panel();
        public PictureBox pictureBox1 = new PictureBox();
        public Label nameLabel = new Label();
        public MyPanel()
        {
            //...
        }
    }
}

This is it. 就是这个。 When I tried to tun it I get this exception : 当我尝试调整它时,出现此异常:

SerializationException was unhandled 未处理SerializationException

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll mscorlib.dll中发生了类型为'System.Runtime.Serialization.SerializationException'的未处理异常

Additional information: 'WindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' Derlemesindeki 'WindowsFormsApplication1.MyPanel' ... 附加信息:'WindowsFormsApplication1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'Derlemesindeki'WindowsFormsApplication1.MyPanel'...

I dont think you will be able to just serialize controls like that. 我认为您不能像这样序列化控件。 I would suggest creating classes around the data you want to save and make the controls only responsible for display. 我建议围绕要保存的数据创建类,并使控件仅负责显示。

[Serializable]
public class MyPanelInfo 
{
    public PanelInfo panel1;
    public PanelInfo panel2;
    public PanelInfo panel3;
    public PanelInfo panel4;
    public Image image;
    public string nameLabel;

    public MyPanelInfo()
    {

    }
}

[Serializable]
public class PanelInfo
{
    public string id;
    public string name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM