Object serialization mainly solves the problem of saving the object state. The "object state" mentioned here refers to the set of Field Values owned by the object at a certain time point.
The main functions of serialization are:
1. Read the information of the last saved object at the next startup of the process.
2. Transmit data between different AppDomains or processes
3. Data Transmission in Distributed Application Systems
......
I. Concepts of Object serialization
OurProgramThe object runs in the memory space, so once the program is closed, these objects will also be destroyed by the CLR garbage collection mechanism. The second program, and again.
If you want to "reproduce" the "scenario" during the first running of the program for the second running, that is, to restore the state of each object during the first running, the application must adopt a certain
The method saves the values of each field of the object to the disk file, so that you can reset the Field Values of the object from the disk file as needed.
The process of saving an object in memory to the stream and re-reading the data from the stream as needed is called "Object serialization" and "deserialization"
Ii. Stream
The storage medium used to save Object serialization is stream.
Stream is an abstract concept that represents a series of Ordered binary data.
For example, an open file can be viewed as a stream.
In. Net, a stream is abstracted as a stream class. This class maintains a read/write pointer internally, which points to the reading point of the stream data. Stream class provides a seek method to move this pointer.
The stream class defines read/readbyte and write/writebyte methods to access data in bytes.
The stream class is an abstract class. In actual development, its subclasses (networkstream, filestream, pipestream, and memorystream) are used ).
Two classes are most used: memorystream representing a memory area and filestream representing a file. The filestream class is selected below for discussion:
In. net, files are accessed through filestream objects.
When an application needs to access a file, it must first create a filestream object, which is one-to-one correspondence with the file.
Accessing file content is equivalent to calling filestream object related methods. The following is a typical example of reading a file contentCode.
Using (filestream reader = new filestream (filename, filemode. Open ))
{
// Call the read ()/readbyte () series methods defined by the stream class to read data from the file ....
}
The read/readbyte and write/writebyte methods defined by the stream class are measured in bytes, which is inconvenient to use in actual development. For example
When a double type data is written to a file, you have to call the writebyte method eight times to write 8 bytes (because a doulbe type data occupies 8 bytes ).
To facilitate access to various common types of streams, the. NET base class Library provides the following two sets of helper classes:
1. binaryreader and binarywrite: used to read and write various basic data types to the stream.
2. streamreader and streamwriter: used to read and write string data from the stream.
These two groups of four classes are much used in actual development. They all need stream objects at the underlying layer to complete real data access.
For example, the following code uses a streamwrite object to write a string to the hello.txt file:
Using (filestream FS = new filestream ("hello.txt", filemode. Create ))
{
Streamwriter writer = new streamwriter (FS );
Writer. writelin ("Hello! ");
Writer. Close ();
}
The reason why we want to introduce the stream is that the data after the object sequence is saved in the stream.