C # serializable Object serialization Function

Source: Internet
Author: User

1. serialization definition: the process of converting an object to a format that is easy to transmit. For example, you can serialize an object and Use http to transmit the object between the client and the server over the Internet. Conversely, deserialization reconstructs an object based on the stream.

In this process, first convert the Public and Private fields of the object and the name of the class (including the Assembly where the class is located) to the byte stream, and then write the byte stream into the data stream. When deserializing the object, a copy identical to the original object will be created.

2. Cause of serialization: Save the object status in the storage media so that identical copies can be created later; send objects from one application domain to another by value. For example, serialization can be used to save session states in ASP. NET and copy objects to the clipboard of Windows Forms. It can also be used to remotely pass objects from one application domain to another by value. This article briefly introduces serialization used in Microsoft. NET.

3. Basic serialization

[Serializable]

Public class myobject {

Public int n1 = 0;

Public int n2 = 0;

Public String STR = NULL;

}

The following code snippet illustrates how to serialize an instance of this type into a file:

Myobject OBJ = new myobject ();

OBJ. n1 = 1;

OBJ. n2 = 24;

OBJ. Str = "some strings ";

Iformatter formatter = new binaryformatter ();

Stream stream = new filestream ("myfile. bin", filemode. Create, fileaccess. Write, fileshare. None );

Formatter. serialize (stream, OBJ );

Stream. Close ();

4. 5. Selective serialization

The class usually contains fields that should not be serialized. For example, assume that a class uses a member variable to store the thread ID. When this class is deserialized, the thread corresponding to the ID stored in the serialization class may no longer run, so serialization of this value is meaningless. You can use the nonserialized attribute to mark member variables to prevent them from being serialized, as shown below:

[Serializable]

Public class myobject

{

Public int N1;

[Nonserialized]

Public int N2;

Public String STR;

}

 

C # Summary of serial number and deserialization

1. C # serialization and deserialization

2. Functions of C # serializable Object serialization (this article)

Reference: http://www.cnblogs.com/winner/archive/2008/03/25/1120757.html

C # serializable Object serialization Function

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.