Serializable (Asp.net C #)

Source: Internet
Author: User

For example, a class is used to describe a contract, and after the class is instantiated, the field in the class stores the contract information, if you want to send the instance of this class to another machine, another form, or want to save this class for later use (persistence object ), this class can be serialized (serialization is actually a stream of information), transmitted or saved, and then deserialized to regenerate this object.

Why do you want to use serialization?

There are two most important reasons for serialization:

One reason is to keep the object state in the storage media so that you can recreate a precise copy later;

Another reason is to use a value to extract an object from an application.ProgramThe domain is sent to another application domain. For example, serialization can be used to save the session state in ASP. NET and copy the object to the clipboard of Windows Forms. Remote processing can also use serialization to pass objects from one application domain to another through values. serialization is the object state, that is, the value of the object data member and the method are irrelevant. serialization and Web services are required for cross-application domain communication.

I. binaryformatter serialization

Serialization is easy to understand, that is, writing memory to the hard disk, or writing it To the memory (this content will be shown in the following example ). deserialization reads information from the hard disk to the memory. that's easy. Now let's look at the following example!

In this articleArticleI will use the binaryformatter serialization class book as an example. I hope you can deeply understand what serialization is.

[Ref = "tag-863-1.html"> serializable]
Public class Book
{
String name;
Float price;
String author;

Public book (string bookname, float bookprice, string bookauthor)
{
Name = bookname;
Price = bookprice;
Author = bookauthor;
}
}

Added the attribute serializable. (If this attribute is not added, a serializationexception will be thrown ).

Mark the book as serializable. of course, there is another way to make the class Book serializable, that is, implementing the iserializable interface. note that the serializable attribute cannot be inherited !!!

If you don't want to serialize a variable, what should you do? It's easy to add the attribute [nonserialized] before it. For example, I don't want to serialize it.

String author;

Then I only need

[Nonserialized]

String author;

Now, let's show you how to implement serialization:

We use namespace:

Using system;

Using system. IO;

Using system. runtime. serialization. formatters. Binary;

First create a book instance, like this:

Book = New Book ("Day and night", numbertype = "1" TCSC = "0"> 30366f, "Bruce ");

Then, of course, we need to create a file, which is used to store the information we want to serialize.

Filestream FS = new filestream (@ "C: \ book. dat", filemode. Create );

Serialization implementation is also very simple, like this:

Binaryformatter formatter = new binaryformatter ();
Formatter. serialize (FS, book );

Easy! Now I will list the entire originalCode, Including deserialization.

Static void main (string [] ARGs)
{
Book = New Book ("Day and night", 30366f, "Bruce ");

Using (filestream FS = new filestream (@ "C: \ book. dat", filemode. Create ))
{
Binaryformatter formatter = new binaryformatter ();
Formatter. serialize (FS, book );
}

Book = NULL;

Using (filestream FS = new filestream (@ "C: \ book. dat", filemode. Open ))
{
Binaryformatter formatter = new binaryformatter ();
Book = (book) formatter. deserialize (FS); // you should note that the returned value is object.
}
}

For example, a class is used to describe a contract, and after the class is instantiated, the field in the class stores the contract information, if you want to send the instance of this class to another machine, another form, or want to save this class for later use (persistence object ), this class can be serialized (serialization is actually a stream of information), transmitted or saved, and then deserialized to regenerate this object.

Why do you want to use serialization?

There are two most important reasons for serialization:

One reason is to keep the object state in the storage media so that you can recreate a precise copy later;

Another reason is that an object is sent from one application domain to another by value. For example, serialization can be used to save the session state in ASP. NET and copy the object to the clipboard of Windows Forms. Remote processing can also use serialization to pass objects from one application domain to another through values. serialization is the object state, that is, the value of the object data member and the method are irrelevant. serialization and Web services are required for cross-application domain communication.

I. binaryformatter serialization

Serialization is easy to understand, that is, writing memory to the hard disk, or writing it To the memory (this content will be shown in the following example ). deserialization reads information from the hard disk to the memory. that's easy. Now let's look at the following example!

In this article, I will use the binaryformatter serialization class book as an example. I hope you can deeply understand what serialization is.

[Ref = "tag-863-1.html"> serializable]
Public class Book
{
String name;
Float price;
String author;

Public book (string bookname, float bookprice, string bookauthor)
{
Name = bookname;
Price = bookprice;
Author = bookauthor;
}
}

Added the attribute serializable. (If this attribute is not added, a serializationexception will be thrown ).

Mark the book as serializable. of course, there is another way to make the class Book serializable, that is, implementing the iserializable interface. note that the serializable attribute cannot be inherited !!!

If you don't want to serialize a variable, what should you do? It's easy to add the attribute [nonserialized] before it. For example, I don't want to serialize it.

String author;

Then I only need

[Nonserialized]

String author;

Now, let's show you how to implement serialization:

We use namespace:

Using system;

Using system. IO;

Using system. runtime. serialization. formatters. Binary;

First create a book instance, like this:

Book = New Book ("Day and night", numbertype = "1" TCSC = "0"> 30366f, "Bruce ");

Then, of course, we need to create a file, which is used to store the information we want to serialize.

Filestream FS = new filestream (@ "C: \ book. dat", filemode. Create );

Serialization implementation is also very simple, like this:

Binaryformatter formatter = new binaryformatter ();
Formatter. serialize (FS, book );

Easy! Now I list the entire original code, including deserialization.

Static void main (string [] ARGs)
{
Book = New Book ("Day and night", 30366f, "Bruce ");

Using (filestream FS = new filestream (@ "C: \ book. dat", filemode. Create ))
{
Binaryformatter formatter = new binaryformatter ();
Formatter. serialize (FS, book );
}

Book = NULL;

using (filestream FS = new filestream (@ "C: \ book. dat ", filemode. open)
{< br> binaryformatter formatter = new binaryformatter ();
book = (book) formatter. deserialize (FS); // here you should note that the returned value is object
}< BR >}

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.