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

ArrayList of objects serialization exception in c#

我有一个 ArrayList,它由我创建的许多对象组成。我正在努力保留它。据我所知,最好的解决方案是使用二进制格式化程序。但是我的代码有问题。无论是写作还是阅读,它都不起作用。这是我的代码;

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

这是我的设备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;
        }
    }
}

这是我的面板 class:

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()
        {
            //...
        }
    }
}

就是这样。当我试图调整它时,我得到了这个异常:

SerializationException was unhandled

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

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

我认为您无法像那样序列化控件。我建议围绕您要保存的数据创建 类,并使控件仅负责显示。

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