Serialized object (.NET)
In fact, it is a very old topic. Sometimes I don't count this. But I haven't made something for a long time.
Just write. But tell the truth, now many online examples are beta versions, I provide official
I thought I still have a certain value.
What is the first to serialize? Simply put the object instance to the method in the disk. So the sequence
The process is the state of the storage object. That is to say to keep the member data as a disk while maintaining the original structure.
Then, for the serialization process, there is also a reverse sequence. I think everyone has guessed, that is, it is already
The serialized object is read. Thought is very simple, yes, then I will come to a small example, try it.
A complete serialization process is clear. So the example is more habit of understanding C #. So I am also
Use C # written, but in nature .NET uses the same set of Framework is also the same set of CTS and
According to the same coding convention, it is consistent,
Then I will introduce the two namespaces to everyone now, because it is used during the encoding process.
System.Runtime.Serialization System.Runtime.Serialization.Formatters.binary
For these two names space, please refer to MSDN: MS-Help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemRuntimeSerization.htm
Ms-help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemRuntimeSerizationFormattersbinary.htm
The example is as follows:
Using system; using system.runtime.serialization; using system.Runtime.Serialization.formatters.binary; public class serialtest {
public void SerializeNow () {YarshrayToSerialize Y = new YarshrayToSerialize (); Stream s = File.Open ( "temp.dat", FileMode.Create); BinaryFormatter b = new BinaryFormatter (); b.Serialize (s, Y); s .Close ();
public void DeSerializeNow () {YarshrayToSerialize Y = new YarshrayToSerialize (); Stream s = File.Open ( "temp.dat", FileMode.Open); BinaryFormatter b = new BinaryFormatter (); Y = (YarshrayToSerialize) b.Deserialize (s Console.writeline ("Name is:" y.
Public static void main (string [] s) {
SERIALTEST ST = New SerialTest (); st.serializenow (); st.deSerializenow ();
}
} [Serializable ()] public class yarshraytoserialize {public int Age = 21; public string name = "yarshray";
}
Last output:
Name is: Yarshrayage IS: 21
Then let's look at this example. We have to look at it? The careful friends may have seen [Serializable ()] this feature. We know that the feature is used in .NET is used to describe objects or methods, and those functions [Serializable ()]
This feature indicates that the object has serialization. (Detailed reference: MSDN
MS-help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemserializableattributemembertopic.htm)
Then let's talk about the steps of serialization again. I first define a class called YarshiToserialize.
Sequence my own information. I joined the feature on this class [Serializable ()], telling Runtime I have this class.
It is necessary to serialize at runtime. Here I will mention it, if you don't [Serializable ()] This feature is then compiled
It can be passed. However, an exception will be thrown when it is run. This means that the serialization is the operation that runs between it. Actually you
The namespace can also be seen. Then I read and write the disk in my serialtest class. Store the information to a TEMP.DAT
In the file, of course this file is my custom, you can define other. Here you use the operation of the stream object and file object.
This is an IO operation. Not in the scope of this article, if you are not clear, you can refer to the .NET's IO operation. I am directly called in the main function.
Two ways. One is serialization, one is anti-sequence. The key code is binaryformatter.serialize (s, y)
And binaryformatter.deSerialize (s) This method is returned by an Object, so I am working here.
Unpacking operation (for unpacking please and mandatory type conversion and this object virtual pointer in a concept. Of course, this is not here
During the scope of discussion).
So next, I want to say the basic steps of the serialized object, because I am just a simple example, if complicated
Operation Its basic steps will not change. Here I think it should be divided into four steps:
1. First I want to create an object instance (that is, this sentence yarshraytoserialize y = new yarshraytoserize ();
Put the class instantiate to an object) The purpose is to save the status of this object to the disk.
2. Need to create a stream object to read and write file objects
3. Need to create a sequential function (here I use binaryformatter, of course, if you serialize
Just XMLFormatter)
4. That is obvious, it is to save the object instance to serialize the SERIALIZE method.
Anti-sequencing and this similar, just turn the Serialize method to this deserialize method. Then the steps are the same.
You can take a look at the above code. So about the topic of serialization, I will talk about it, then the complex class is basically
The step is the same. You can add attributes and methods.
Finally, talk out, serialization technology has been encapsulated by a variety of Framework, and provides corresponding functional calls. This is an object
The permanent preservation is very obvious. Of course, you must support dynamic creation in the reverse sequence process or no meaning.
What it is.