. Net failed to serialize the BinaryFormatter object with events.

Source: Internet
Author: User

An existing object (objectA) needs to be serialized into a binary file using BinaryFormatter:

FileStream fileStream = new FileStream (path, FileMode. Create );

BinaryFormatter formatter = new BinaryFormatter ();

Formatter. Serialize (fileStream, objectA );

The object objectA is defined as follows:


[Serializable ()]
Public class ObjectA
{
Public string Name
{Set; get ;}
Public event EventHandler <EventArgs> Created;

}

Note: The object ObjectA has an event: Created. If you subscribe to the ObjectA Created event in an object that cannot be serialized (such as in WinForm), an error will occur during serialization:
 

 


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;

Namespace BinarySerializer
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void button#click (object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog ();
If (dialog. ShowDialog () = System. Windows. Forms. DialogResult. OK)
{
Try
{
ObjectA objectA = new ObjectA {Name = ""};
If (checkBox1.Checked)
ObjectA. created + = new EventHandler <EventArgs> (objectA_Created); // when an object is serialized, if the object event is subscribed to by another object, a serialization failure error occurs, if this statement is deregistered, the serialization is successful.
String path = dialog. FileName;
FileStream fileStream = new FileStream (path, FileMode. Create );
BinaryFormatter formatter = new BinaryFormatter ();
Formatter. Serialize (fileStream, objectA );
MessageBox. Show ("saved successfully ");
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
}

Void objectA_Created (object sender, EventArgs e)
{
Throw new NotImplementedException ();
}
}

[Serializable ()]
Public class ObjectA
{
Public string Name
{Set; get ;}
Public event EventHandler <EventArgs> Created;

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.