Serialization and persistence in. Net (1)

Source: Internet
Author: User

This article from: http://www.cnblogs.com/lmm0591/archive/2011/09/21/2180836.html

 

 

I am very simple in programming and have only two or three years of experience. So I will not go deep into the content of this article. I will only offer it to cainiao, so the old birds should not laugh at me. I welcome the old birds to point out the problem, you are also welcome to discuss this.

I writeArticleThere are two purposes: one is to consolidate and digest what you have learned, and the other isProgramApe discussion.

------------------------------------------------- Nonsense split line -------------------------------------------------

Everyone knows. many States (member variables) exist in the object. When we want to save the object, we need to store the values through the database, in the next use, retrieve the value from the database and assign a value to the image. Developers need to write a large string of mechanicalCode, You cannot send effective time to the business. Now we will introduce the function of Object serialization to serialize objects into binary or soap format, when you need to use the object state next time, you can directly deserialize the object to generate the object.

If you have said so much, let's look at the example.

[Serializable]
Public ClassMyclass
{
Public StringName
{
Get;
Set;
}


Public IntAge
{
Get;
Set;
}
}

Adding a [serializable] attribute to the image tells the compiler that the class can be serialized,

The client also needs to use the binaryformatter class in the namespace system. runtime. serialization. formatters. Binary to write the stream of myclass objects serialized into binary files to the files.

Client example

Myclass my = New Myclass ();
My. Name =" Fengjie " ;
My. Age = Int . Maxvalue;
System. runtime. serialization. iformatter formatter = New Binaryformatter ();

// Create a file stream
Stream stream = New Filestream ( @" C: \ myclass. Bin " , Filemode. Create, fileaccess. Write );

Using (Stream)
{
// Serialization is performed here.
Formatter. serialize (stream, my );
}

Hahaha OK, and the object is serialized. When your program is closedC: \ myclass. BinThe file is deserialized, And the nam value of the deserialized object is stillFengjie, Age value orInt. Maxvalue

Now I will introduce how to deserialize

Client example

  //   A reference to the image is provided after deserialization is not needed   
myclass my;
system. runtime. serialization. iformatter formatter = New binaryformatter ();
// open a file stream
stream = New filestream ( @" C: \ myclass. bin ", filemode. open, fileaccess. read);
using (Stream)
{< br> // This is deserialization
my = (myclass) formatter. deserialize (Stream);
}

Now the simplest serialization and deserialization are all completed. We can see that we do not need to retrieve the data structure image from the database in the database, serialization functions are more convenient in some special scenarios.

Here I will only introduce the value type of the variable. Next I will introduce the serialization, non-serialization, and serialization of the reference type into the SOAP format.

 

I am very simple in programming and have only two or three years of experience. So I will not go deep into the content of this article. I will only offer it to cainiao, so the old birds should not laugh at me. I welcome the old birds to point out the problem, you are also welcome to discuss this.

I write articles for two purposes: one is to consolidate and digest what I have learned, and the other is to discuss with a wide range of programmers.

------------------------------------------------- Nonsense split line -------------------------------------------------

Everyone knows. many States (member variables) exist in the object. When we want to save the object, we need to store the values through the database, in the next use, retrieve the value from the database and assign a value to the image. Developers need to write a large string of mechanical code, so they cannot send effective time to the business. Now we will introduce the function of Object serialization to serialize objects into binary or soap format, when you need to use the object state next time, you can directly deserialize the object to generate the object.

If you have said so much, let's look at the example.

[Serializable]
Public ClassMyclass
{
Public StringName
{
Get;
Set;
}


Public IntAge
{
Get;
Set;
}
}

Adding a [serializable] attribute to the image tells the compiler that the class can be serialized,

The client also needs to use the binaryformatter class in the namespace system. runtime. serialization. formatters. Binary to write the stream of myclass objects serialized into binary files to the files.

Client example

Myclass my = New Myclass ();
My. Name =" Fengjie " ;
My. Age = Int . Maxvalue;
System. runtime. serialization. iformatter formatter = New Binaryformatter ();

// Create a file stream
Stream stream = New Filestream ( @" C: \ myclass. Bin " , Filemode. Create, fileaccess. Write );

Using (Stream)
{
// Serialization is performed here.
Formatter. serialize (stream, my );
}

Hahaha OK, and the object is serialized. When your program is closedC: \ myclass. BinThe file is deserialized, And the nam value of the deserialized object is stillFengjie, Age value orInt. Maxvalue

Now I will introduce how to deserialize

Client example

  //   A reference to the image is provided after deserialization is not needed   
myclass my;
system. runtime. serialization. iformatter formatter = New binaryformatter ();
// open a file stream
stream = New filestream ( @" C: \ myclass. bin ", filemode. open, fileaccess. read);
using (Stream)
{< br> // This is deserialization
my = (myclass) formatter. deserialize (Stream);
}

Now the simplest serialization and deserialization are all completed. We can see that we do not need to retrieve the data structure image from the database in the database, serialization functions are more convenient in some special scenarios.

Here I will only introduce the value type of the variable. Next I will introduce the serialization, non-serialization, and serialization of the reference type into the SOAP format.

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.