NEWS INFORMATION

新闻资讯

.net带事件的对象BinaryFormatter 序列化失败

作者:admin 发布时间:2019-12-07

现有一个目标需要用BinaryFormatter序列化成二进制文件:

FileStream fileStream = new FileStream;

BinaryFormatter formatter = new BinaryFormatter;

formatter.Serialize;

目标objectA的界说如下:


 [Serializable]
  public class ObjectA
  {
  public string Name
  { set; get; }
  public event EventHandler EventArgs Created;







  }

留意:目标ObjectA有一个事情:Created.假如在不能被序列化的目标订阅了ObjectA的Created事情,那么在序列化时,就会犯错:
 


带事情的目标序列化失利Demo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

带事情的目标序列化失利Demo









namespace BinarySerializer
{
public partial class Form1 : Form
{
public Form1
{
InitializeComponent;
}








private void button1_Click
{
SaveFileDialog dialog = new SaveFileDialog;
if == System.Windows.Forms.DialogResult.OK)
{
try
{
ObjectA objectA = new ObjectA { Name = A };
if
objectA.Created += new EventHandler EventArgs ;//目标在被序列化时,假如目标的事情被其他目标订阅了,则会呈现序列化失利过错,将这句刊出就序列化成功。
string path = dialog.FileName;
FileStream fileStream = new FileStream;
BinaryFormatter formatter = new BinaryFormatter;
formatter.Serialize;
MessageBox.Show
{
MessageBox.Show;
}
}
}




















void objectA_Created
{
throw new NotImplementedException;
}
}





[Serializable]
public class ObjectA
{
public string Name
{ set; get; }
public event EventHandler EventArgs Created;






 

Ruby实现的最优二叉查找树算法
下一篇:没有了
扫一扫,关注我们

友情链接 :
 
地址:江苏省南京市玄武区玄武湖
电话:
邮箱:9490489@qq.com
Q Q:9490489