c#中对象序列化异常的ArrayList(ArrayList of objects serialization exception in c#)

编程入门 行业动态 更新时间:2024-10-28 00:16:42
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; } } }

这是我的设备类:

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

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

就是这个。 当我试图调试它时,我得到了这个例外:

SerializationException未处理

mscorlib.dll中发生未处理的“System.Runtime.Serialization.SerializationException”类型异常

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

I have an ArrayList which consist of lots of object created by me. 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:

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

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

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

更多推荐

本文发布于:2023-07-28 18:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   对象   序列化   ArrayList   serialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!