The so-called serialization is the process of converting an object to a format that is easy to transmit. In general, the object is converted into a stream file and put into memory or IO files. For example, you can serialize an object and Use http to transmit the object between the client and server over the Internet, or Program Shared usage. Conversely, deserialization reconstructs an object based on the stream.
. Net comes with two methods to serialize objects: XML and binary. XML serialization does not convert methods, indexers, private fields, or read-only attributes (except for read-only sets ). To serialize all fields and attributes of an object (public and private), use binaryformatter instead of XML serialization (see ms-help: // Ms. netframework. v20.chs/dv_fxserialization/html/8c63200d-db63-4a03-a93d-21641623df62.htm
XML and soap serialization ).
The Processing Methods of the two programs are basically the same, both of which are based on the factory mode. Below I will only talk about the binary serialization method:
For example, we have an object:
[Serializable] Public class classtoserialize {
Public int id = 100;
Public string name = "name ";
}
To serialize this object, you must add the serializable attribute to the class and create a stream to be serialized and written:
Filestream = new filestream ("Temp. dat", filemode. Create );
Then create a binary formatter:
Binaryformatter B = new binaryformatter ();
Then there is serialization:
B. serialize (filestream, C );
And close the Save stream. (See the example below)
When reading a serialized object: The operation method is the same,
Filestream = new filestream ("Temp. dat", filemode. Open, fileaccess. Read, fileshare. Read );
Classtoserialize c = (classtoserialize) B. deserialize (filestream );
Then you can read it. The complete example is:
Using system;
Using system. IO;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Public class serialtest {
Public void serializenow (){
Classtoserialize c = new classtoserialize ();
Filestream = new filestream ("Temp. dat", filemode. Create );
Binaryformatter B = new binaryformatter ();
B. serialize (filestream, C );
Filestream. Close ();
}
Public void deserializenow (){
Classtoserialize c = new classtoserialize ();
Filestream = new filestream ("Temp. dat", filemode. Open, fileaccess. Read, fileshare. Read );
Binaryformatter B = new binaryformatter (); // soapformatter
C = (classtoserialize) B. deserialize (filestream );
Console. writeline (C. Name );
Filestream. Close ();
}
Public static void main (string [] S ){
Serialtest ST = new serialtest ();
St. serializenow ();
St. deserializenow ();
}
}
[Serializable]
Public class classtoserialize {
Public int id = 100;
Public string name = "name ";
}
This is the built-inSerialization and deserializationBut, in many cases, an object is relatively large, and many private attributes and methods are not required, such as serialization in the prototype mode, only the clone method and some attributes are required. The private method is not required. For example, when reading large-scale Io, the read operation is completely unnecessary... at this time, you need to integrate the iserializable interface of the rewrite sequence:
You need to pay attention to the implementation of this interface. One is the constructor, mainly for deserialization, and the other is getobjectdata, mainly for serialization. For example, we now have an employee class that needs to be serialized.
[Serializable ()] // set this attribute to all the classes that want to serialize
Public class employee: iserializable // derive your class from iserializable {
Public int empid;
Public String empname;
[Nonserialized ()]
Public String noserialstring = "noserialstring-test ";
}
Note that [nonserialized ()] exists before the noserialstring attribute, which means that this attribute is not serialized by default, but the default value is used.
The first is the constructor:
Public Employee (serializationinfo info, streamingcontext ctxt)
{
Empid = (INT) info. getvalue ("employeeid", typeof (INT ));
Empname = (string) info. getvalue ("employeename", typeof (string ));
// Noserialstring = (string) info. getvalue ("noserialstring", typeof (string ));
}
Then there is the serialization method, that is, how to save it when writing a stream: Public void getobjectdata (serializationinfo info, streamingcontext ctxt)
{
// You can use any custom name for your name-value pair. But make sure you
// Read the values with the same name. For EX:-If you write empid as "employeeid"
// Then you shoshould read the same with "employeeid"
Info. addvalue ("employeeid", empid );
Info. addvalue ("employeename", empname );
}
Write the above two methods into the employee class, and then write a test program: public class objserial {
Public static void main (string [] ARGs ){
Employee MP = new employee ();
MP. empid = 10;
MP. empname = "omkumar ";
MP. noserialstring = "hello ";
// Serialization
Stream stream = file. Open ("employeeinfo. OSL", filemode. Create );
Binaryformatter bformatter = new binaryformatter ();
Console. writeline ("Writing employee information ");
Bformatter. serialize (stream, MP );
Stream. Close ();
MP = NULL;
// Reverse Sequence
Stream = file. Open ("employeeinfo. OSL", filemode. Open );
Bformatter = new binaryformatter ();
Console. writeline ("reading employee information ");
MP = (employee) bformatter. deserialize (Stream );
Stream. Close ();
Console. writeline ("employee ID: {0}", MP. empid. tostring ());
Console. writeline ("employee name: {0}", MP. empname );
Console. writeline ("Maid: {0}", MP. noserialstring );
}
}
The execution result is:
Writing employee information
Reading employee information
Employee ID: 10
Employee name: omkumar
Employee noserialstring: noserialstring-test
We can see that the value of the employee noserialstring property does not exist. It retains the default value and is not serialized.
Serialization is very simple, but it is often used. Write it here for the time being.