Serialization in. NET (1)

Source: Internet
Author: User

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 class MyClass {public string Name {get; set;} public int Age {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 = new FileStream (@ "c: \ MyClass. bin ", FileMode. create, FileAccess. write); using (stream) {// The formatter is serialized here. serialize (stream, my);} hahaha OK. The object is serialized. When your program is closed, use c: \ MyClass next time. bin file deserialization is good. The Nam value of the deserialization object is still Fengjie, and the Age value is still int. maxValue

Now I will introduce how to deserialize

Client example

// A reference to the object MyClass my; System will be given after deserialization is not needed. runtime. serialization. IFormatter formatter = new BinaryFormatter (); // open a file Stream stream Stream = new FileStream (@ "c: \ MyClass. bin ", FileMode. open, FileAccess. read); using (stream) {// here is the deserialization of my = (MyClass) formatter. deserialize (stream);} Now the simplest serialization and deserialization are all described. We can see that we do not need to take 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.

Author: "humane programming"
 

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.