Serializable serialization in C,

Source: Internet
Author: User

Serializable serialization in C,

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 share the object with other applications. Conversely, deserialization reconstructs an object based on the stream.

I. Several serialization Technologies

1) binary serialization maintains type fidelity, which is useful for retaining the state of objects between different calls of an application. For example, objects can be shared between different applications by serializing objects to the clipboard. You can serialize an object to a stream, disk, memory, or network. Remote Processing uses serialized "pass through value" to pass objects between computer or application domains.

2) XML serialization only serializes public attributes and fields without retaining type fidelity. This is useful when you want to provide or use data without limits on applications that use the data. Because XML is an open standard, it is a good choice for sharing data through the Web. SOAP is also an open standard, making it an attractive choice.

3) use the provided data protocols to serialize and deserialize type instances into XML streams or documents (or JSON format ). It is often used for WCF communication.

 

Ii. serialization Classification

 

1. Basic serialization

To make a class Serializable, the simplest way is to mark it with the Serializable attribute, as shown below:

C # code Replication
[Serializable]
public class MyObject
{   public int n1 = 0;   public int n2 = 0;   public String str = null;}

 

Serialize an instance of the above class into a file

C # code Replication
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();

 

Deserialization of the above instance

C # code Replication
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile。bin", FileMode.Open,
FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(fromStream);
stream.Close();

 

If portability is required, use SoapFormatter. All you need to do is change the formatting program in the above Code to SoapFormatter, while the calls of Serialize and Deserialize remain unchanged.

 

Note that the Serializable attribute cannot be inherited. If a new class is derived from MyObject, the new class must also be marked with this attribute; otherwise, serialization will fail. For example, if you try to serialize the following class instances, a SerializationException is displayed, indicating that the MyStuff type is not marked as serializable.

 

2. 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:

C # code Replication
[Serializable]
public class MyObject
{   public int n1;   [NonSerialized]   public int n2;   public String str;}

 

3. Custom serialization

You can customize the serialization process by implementing the ISerializable interface on the object. This function is especially useful when the value of the member variable becomes invalid after deserialization, but you need to provide the value for the variable to recreate the complete state of the object. To implement ISerializable, you must implement the GetObjectData method and a special constructor. This constructor is used in deserialization of objects. The following code example shows how to implement ISerializable on the MyObject class mentioned in the previous section.

C # code Replication
[Serializable]
public class MyObject : ISerializable
{   public int n1;   public int n2;   public String str;   public MyObject()   {   }   protected MyObject(SerializationInfo info, StreamingContext context)   {     n1 = info.GetInt32("i");     n2 = info.GetInt32("j");     str = info.GetString("k");   }   public virtual void GetObjectData(SerializationInfo info,StreamingContext context)   {     info.AddValue("i", n1);     info.AddValue("j", n2);     info.AddValue("k", str);   }}

 

When calling GetObjectData during serialization, you need to fill in the SerializationInfo object provided in the method call. You only need to add the variable to be serialized in the form of name/value pairs. The name can be any text. As long as the serialized data is sufficient to restore the object during the deserialization process, you can freely select the member variable added to SerializationInfo. If the base object implements ISerializable, the derived class should call the GetObjectData method of the base object.

It must be emphasized that when adding ISerializable to a class, GetObjectData and special constructors must be implemented at the same time. If GetObjectData is missing, the compiler sends a warning. However, the constructor cannot be implemented forcibly, so no warning is given when the constructor is missing. If you try to deserialize a class without a constructor, an exception will occur. In terms of eliminating potential security and version control problems, the current design is better than the SetObjectData method. For example, if you define the SetObjectData method as part of an interface, this method must be a public method, so that you have to write code to prevent multiple calls to the SetObjectData method. As you can imagine, if an object is performing some operations, but a malicious application calls the SetObjectData method of this object, it will cause some potential troubles.

In the deserialization process, use the constructor provided for this purpose to pass SerializationInfo to the class. During object deserialization, any visibility constraints on constructors are ignored. Therefore, classes can be marked as public, protected, internal, or private. A good way is to mark the constructor as protect if the class is not encapsulated. If the class has been encapsulated, it should be marked as private. To restore the object state, you only need to use the name used during serialization to retrieve the value of the variable from SerializationInfo. If the base class implements ISerializable, you should call the base class constructor so that the base object can restore its variables.

If a new class is derived from the class that implements ISerializable, the constructor and the GetObjectData method must be implemented as long as the new class contains any variable to be serialized. The following code snippet shows how to use the MyObject class shown above to complete this operation.

C # code Replication
[Serializable]
public class ObjectTwo : MyObject
{   public int num;   public ObjectTwo() : base()   {   }   protected ObjectTwo(SerializationInfo si, StreamingContext context) :base(si,context)   {     num = si.GetInt32("num");   }   public override void GetObjectData(SerializationInfo si,StreamingContext context)   {     base.GetObjectData(si,context);     si.AddValue("num", num);   }}

 

Remember to call the base class in the deserialization constructor. Otherwise, the constructor on the base class will never be called, and the complete object cannot be constructed after deserialization. It is very easy to retrieve keyword/value pairs during deserialization. However, the classes derived from the hash cannot be deserialized, therefore, some problems may occur when these objects are added to the return list. Therefore, we recommend that you do not call methods on the hash table.

 

 

3. Methods for changing the object status in different versions1. Implement ISerializable. This allows you to precisely control the serialization and deserialization processes and correctly add and interpret future states during the deserialization process.

2. Use the NonSerialized attribute to mark unimportant member variables. This option can be used only when the expected class changes between different versions. For example, after adding a new variable to a higher version of the class, you can mark the variable as NonSerialized to ensure the class is compatible with earlier versions.


CC real name

The story of LULU and King Arthur and the real name Analysis of CC
According to a strong analysis on the Internet.
Although lancot appeared in the first season
But he never imagined it with Arthur.
Because it is the name of the body, I don't think much about it.

Now I saw R2. seeing the name of the Round Table knight, the king opened his eyes with a pair of geass.
Only then can we realize that the essence of ruuxiu is actually a variant of Arthur's story...
If this is true, the true name of c. c is clear ..

The introduction of the Royal Family Name "briannia" refers to a relatively old saying in Britain. The "British kings" One hundred and fifty years ago used the British annia, and this book refers to the story of King Arthur's round table knight, the number of Round Table Knights ranges from.
In this tutorial, the Round Table server has a minimum of 12

In the legend of King Arthur, lancelot is a knight who betrayed the king because he fell in love with the king.
And he is an orphan of a young father

Let's look at the name of zhuque Shu Mu.
Pivot means key or important.
Zhuque now seems to be no different from the species of birds that can survive the burning of death.
So: OK: zhumu zhuque is a young father who will betray with love as an excuse. It plays an important role in this film, and how can he play an undead role...

King Arthur disappeared after a duel with his son mordred (in fact, he died). The reason for the duel is that this son wants to gain a throne. Please note that, king Arthur knew that his son would have killed him shortly after his birth (a legend in the west) but did not kill him. Instead, he continued to raise him.

Ruuxiu's name is very interesting.
Japanese is written in Japanese.
However, the official western style is: Lelouch Lamperouge
Let's take a look at this sentence.
Le louche explains abnormal behavior in the eyes and greedy behavior in the eyes.
Lampe rouge is a red light or lamp.
The son who killed Arthur was named Mordred.
The split shows that Mord is related to death + Red
OK: Lu luxiu is an eye that emits abnormal red light and greedy character (every time I play tactics, I want to use all the methods, and the result is reversed by the enemy's strong role ), sending is spreading the message of death to the prince who targeted his father.

Kalian
The old surname "Red Moon" in Area 11 refers to ominous things. Normally, attackers like to mount the moon at night.
The last name of the empire is Stadtfield, which indicates the field of the municipal council.
Her name, Kallen, was directly translated into Karine or Hualien for reference by the translation team.
In fact, this word means that it is a verb to hold it tightly with claws.
Everyone should know the explanation of her body, that is, the spark of a huge explosion.

OK: So cailian is a very honorable and non-invasive entity in the empire. However, it is always carrying out ominous activities in the dark. It is a fire of character and is good at operating a body that can hold the enemy tightly with claws.

So what?
Name of c. c. As a key character
It will certainly be associated with her miserable situation.
And it must be in touch with Arthur's background.
Because c. c. once belonged to the holy kingdom of Britain and then escaped.
That is to say, Arthur got and lost things.
This is not a human thing for Arthur.
And it can be related to the letter C.

Then only Arthur's first sword is Calibur.
The other name is Sword of the Stone before King Arthur gets it.
Later, it was broken in a duel that does not conform to the spirit of the server guard.

Caliburia is highly likely to become a personal name.
Considering that c. c. is a girl, it may be changed
Caliburian or Clirburess
Because C. C.'s real name is five syllables (11 episodes in the first season)
And... the remaining full text>

CC real name

The story of LULU and King Arthur and the real name Analysis of CC
According to a strong analysis on the Internet.
Although lancot appeared in the first season
But he never imagined it with Arthur.
Because it is the name of the body, I don't think much about it.

Now I saw R2. seeing the name of the Round Table knight, the king opened his eyes with a pair of geass.
Only then can we realize that the essence of ruuxiu is actually a variant of Arthur's story...
If this is true, the true name of c. c is clear ..

The introduction of the Royal Family Name "briannia" refers to a relatively old saying in Britain. The "British kings" One hundred and fifty years ago used the British annia, and this book refers to the story of King Arthur's round table knight, the number of Round Table Knights ranges from.
In this tutorial, the Round Table server has a minimum of 12

In the legend of King Arthur, lancelot is a knight who betrayed the king because he fell in love with the king.
And he is an orphan of a young father

Let's look at the name of zhuque Shu Mu.
Pivot means key or important.
Zhuque now seems to be no different from the species of birds that can survive the burning of death.
So: OK: zhumu zhuque is a young father who will betray with love as an excuse. It plays an important role in this film, and how can he play an undead role...

King Arthur disappeared after a duel with his son mordred (in fact, he died). The reason for the duel is that this son wants to gain a throne. Please note that, king Arthur knew that his son would have killed him shortly after his birth (a legend in the west) but did not kill him. Instead, he continued to raise him.

Ruuxiu's name is very interesting.
Japanese is written in Japanese.
However, the official western style is: Lelouch Lamperouge
Let's take a look at this sentence.
Le louche explains abnormal behavior in the eyes and greedy behavior in the eyes.
Lampe rouge is a red light or lamp.
The son who killed Arthur was named Mordred.
The split shows that Mord is related to death + Red
OK: Lu luxiu is an eye that emits abnormal red light and greedy character (every time I play tactics, I want to use all the methods, and the result is reversed by the enemy's strong role ), sending is spreading the message of death to the prince who targeted his father.

Kalian
The old surname "Red Moon" in Area 11 refers to ominous things. Normally, attackers like to mount the moon at night.
The last name of the empire is Stadtfield, which indicates the field of the municipal council.
Her name, Kallen, was directly translated into Karine or Hualien for reference by the translation team.
In fact, this word means that it is a verb to hold it tightly with claws.
Everyone should know the explanation of her body, that is, the spark of a huge explosion.

OK: So cailian is a very honorable and non-invasive entity in the empire. However, it is always carrying out ominous activities in the dark. It is a fire of character and is good at operating a body that can hold the enemy tightly with claws.

So what?
Name of c. c. As a key character
It will certainly be associated with her miserable situation.
And it must be in touch with Arthur's background.
Because c. c. once belonged to the holy kingdom of Britain and then escaped.
That is to say, Arthur got and lost things.
This is not a human thing for Arthur.
And it can be related to the letter C.

Then only Arthur's first sword is Calibur.
The other name is Sword of the Stone before King Arthur gets it.
Later, it was broken in a duel that does not conform to the spirit of the server guard.

Caliburia is highly likely to become a personal name.
Considering that c. c. is a girl, it may be changed
Caliburian or Clirburess
Because C. C.'s real name is five syllables (11 episodes in the first season)
And... the remaining full text>

Related Article

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.