Today, we are going to learn about the important technology "serialization" that is often used in component development ".
Serialization is not easy to understand and abstract for beginners. Let's use traditional word segmentation to explain what it means.
A mathematical sequence is an object (or event) in a column. In this way, each element is not prior to other elements, but after other elements. Here, the order between elements is very important.
Then, let's take a look at the meaning of serialization in our program against this explanation. We all know that the object state is stored in the memory in real time. The object state is allocated by the system during initialization, and some modifications may be made to the object during the later running process, so how can we save these statuses for future use. [Wang qingpei has all rights reserved. For more information, please sign it.]
. NET serialization is to convert the state of objects in the memory into a regular sequence. Such a sequence can be binary, XML, or SOAP .. NET also provides interfaces that we can implement serialization on our own.
In. NET, we can easily serialize objects through the tools provided by the system. So what is the purpose of serialization? The title of the article mentions the term "persistence". What is persistence?
Persistence: stores data (such as objects in the memory) to a permanent storage device (such as a disk ). The main application of persistence is to store the objects in memory in a relational database. Of course, they can also be stored in Disk Files and XML data files.
By serializing the object state for persistence, we can easily restore the object when necessary. Let's stop talking about the theory. Let's see how the code is implemented.
Serializable features
Using System. Runtime. InteropServices;
Namespace System
{
// Summary:
// Indicates that a class can be serialized. This class cannot be inherited.
[ComVisible (true)]
[AttributeUsage (AttributeTargets. Class | AttributeTargets. Struct | AttributeTargets. Enum | AttributeTargets. Delegate, Inherited = false)]
Public sealed class SerializableAttribute: Attribute
{
// Summary:
// Initialize a new instance of the System. SerializableAttribute class.
Public SerializableAttribute ();
}
}
This is the definition of the Serializable feature. You can use the Serializable feature to mark classes, structures, enumerations, and delegation. The formatter can be used for serialization. objects not marked by the Serializable feature cannot be serialized. An exception is thrown during serialization.
IFormatter Interface
The iformatter interface is a serialized formatter that provides the serialization function. The serialization function objects (BinaryFormatter and SoapFormatter) provided by the system all implement this interface.
Figure 1:
Another XmlSerializer serialization object is in the XML namespace, mainly the highly scalable interface. We can extend it for complicated XML serialization. The two Iformatter interface implementation classes (Binarymatter and SoapMatter) have already helped us implement complicated binary serialization and Soap serialization. We just need to simply use them.
Use the Serializable feature and the IFormatter interface to serialize objects
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. Serialization;
Namespace ConsoleApplication1. serialization and persistence
{
[Serializable]
Public class MyClass
{
Public MyClass (){}
Public string number = "MyClass status ";
}
}
Binary serialization:
IFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream ("obj. bin", FileMode. Create, FileAccess. Write );
Using (stream)
{
Formatter. Serialize (stream, new MyClass ());
}
// Deserialization
Stream stream1 = new FileStream ("obj. bin", FileMode. Open, FileAccess. Read );
Using (stream1)
{
MyClass mycalss = formatter. Deserialize (stream1) as MyClass;
}
SOAP serialization:
IFormatter formatter = new SoapFormatter ();
Stream stream = new FileStream ("obj. xml", FileMode. Create, FileAccess. Write );
Using (stream)
{
MyClass myclass = new MyClass ();
Formatter. Serialize (stream, myclass );
}
Stream stream1 = new FileStream ("obj. xml", FileMode. Open, FileAccess. Read );
Using (stream1)
{
MyClass myclass = formatter. Deserialize (stream1) as MyClass;
}
When using the Iformatter interface to serialize an object, we only need to provide the Stream object. You can serialize objects to FileStream, MemoryStream, and NetworkStream.
Disable member serialization using NonSerialized
In the object, we may need to disable some members from being serialized. During the serialization of an object, the system is a member of the recursive serialization object. If an object is not allowed to be serialized, the Serializable feature is not added. The serialization will fail. At least we need to be able to control its serialization process manually. See the Code:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace ConsoleApplication1. serialization and persistence
{
Public class MyChildClass
{
Public string name = "MyChildClass status ";
}
}
I have not added the serialization feature mark for this object.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. Serialization;
Namespace ConsoleApplication1. serialization and persistence
{
[Serializable]
Public class MyClass
{
Public string number = "MyClass status ";
Public MyChildClass ChildClass;
}
}
Here I need to serialize the MyChildClass object.
Figure 2:
An error is reported here.
In this case, the object MyChildClass field is marked with the deserialization prohibited flag.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. Serialization;
Namespace ConsoleApplication1. serialization and persistence
{
[Serializable]
Public class MyClass
{
Public string number = "MyClass status ";
[NonSerialized]
Public MyChildClass ChildClass;
}
}
Figure 3:
Serialization is successful.
This article mainly introduces some basic concepts and usage of serialization. In the next article, we will learn how to control the serialization process into the serialization.
Author: "Deep training (DotNet session )"