. Net serialization and deserialization,. net serialization
1. serialization deserialization
In C #, if you need to: store objects of classes with complex structures, or transmit objects to remote client programs over the network, serialization is required, deserialization (Serialization & Deserialization)
2. BinaryFormattter
In. NET, there are three serial types: BinaryFormatter, SoapFormatter, and XmlSerializer.
Among them, BinaryFormattter is the simplest. It directly serializes or deserializes an Object in binary mode. Its advantage is that it is fast and CAN serial private or protected member, in different versions. . NET is compatible and can be viewed. NET's own life-saving method, of course, the shortcomings will also come, and leave. . NET.
3. serialization
BinaryFormatter ser = new BinaryFormatter ();
MemoryStream MS = new MemoryStream ();
Ser. Serialize (MS, DS );
Byte [] buffer = ms. ToArray ();
MemoryStream: Creates a stream that supports the memory storage zone.
4. deserialization
// Deserialization: Put byte [] data into Stream, and BinaryFormatter deserializes the data in the Stream into an object
MemoryStream MS = new MemoryStream (bytes );
BinaryFormatter ser = new BinaryFormatter ();
DataSetSurrogate dss = ser. Deserialize (MS) asDataSetSurrogate;
5. Summary
For serialization and deserialization, BinaryFormate is used, and the MemoryStream of the normal stream must be used. The difference is:
During serialization, the object is serialized and put into MemoryStream. During deserialization, the byte [] data in MemoryStream is deserialized into an object.
- SerializeUtil.rar (571 Bytes)
Net serialization and deserialization
I can't see your code. I don't know how to write it. I provide my own serialization method.
Before the class to be serialized, add the [Serializable] Keyword:
// Instantiate MemoryStream
MemoryStream memoryStream = new MemoryStream ();
// Instantiate Formatter
BinaryFormatter formatter = new BinaryFormatter ();
// The serialization class is a base64 string
Formatter. Serialize (memoryStream, this );
// Set the location in the file stream
MemoryStream. Position = 0;
// Deserialization. In your instance, memoryStream is the serialized string.
Formatter. Deserialize (memoryStream );
Deserialize returns an object type, which can be forcibly converted to the original class.
I am using this in practice. If you cannot use it, paste the code.
What are the methods for serialization and deserialization? What are them?
As of. NET 4.0, there are a total of the following serialization classes:
1. xml serialization XmlSerializer
2. binary serialization BinaryFormatter
3. Soap serialization SoapFormatter
4. WCF serialization DataContractSerializer
5. Json serialization DataContractJsonSerializer and JavascriptSerializer