The sequence of the title .NET selects from Possible_y's Blog keyword .NET, serialization, and serialization what is serialization? ---. NET runtime environment is used to support the fluidization of user-defined types. It is the process of storeing the status of the object instance to the storage medium. In this process, first convert the public field of the object and the private field and the name of the class (including the assembly of the class) into a byte stream, and then write the byte stream into the data stream. When the object is then reorganized, a copy of the same identical object is created. Device for serialization: 1. Custom objects are held in a storage form; 2, the object is passed from one place to another. Substant serialization mechanisms are converted to a general (ie continuous) byte stream, and then the stream can be written to disk files or any other fluidized target. To write this stream, you must use the Serialize and DeserialIze methods in classes that implement the IFORMATTER interface. The two classes are provided in the .NET framework: 1. BinaryFormatter BinaryFormatter serializes the binary format. Simply create an instance of the stream and formatting program to use, then call the SERIALIZE method for the formatting program. The flow and the sequential object instance is supplied to this call as a parameter. All member variables in the class (or even variables marked as private) will be serialized. First we create a class: [serializable] public class myObject {public int n1 = 0; public int N2 = 0; public string str = null;} The serializable property is used to explicitly indicate that the class can be serialized. Similarly, we can use the nonserializable attribute to clearly express classes that cannot be serialized. Then we create an instance of this class, then serialize, and save your lasting: 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 (); It is also very easy to restore it. First, create a formatting program and stream for reading, and then let the formatting procedure against objects. IFormatter formatter = new BinaryFormatter (); Stream stream = new FileStream ( "MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read); MyObject obj = (MyObject) formatter.Deserialize (fromStream); stream.Close ( ); // The following is to prove console.writeline ("N1: {0}", obj.n1); console.writeline ("N2: {0}", obj.n2); console.writeline ("str: {0 } ", obj.str); Second, SOAPFORMATTER We use binaryformatter to serialize in binary format.
It is easy for us to change the previous example to SOAPFormatter, which will be formatted in XML, so there is better portability. The changes you want to do are simply convert the formatted program in the above code to SOAPFORMATTER, while Serialize and DeserialIze calls unchanged. For examples used above, the formatter will generate the following results.
We changed the previous MyObject class as: public class myobject {public inters, public string str; public myObject () {} public myobject () {this.n1 = n1; excitation = str;} public override String toString () {Return String.Format ("{0}: {1}", this.str, this.n1);} Now we use the XMLSerializer class to serialize the modified MyObject. Since there is a Type parameter in the constructor of the XMLSerializer class, the XMLSerializer object is clearly connected to the class represented by the TYPE parameter. XmlSerializer class Serialize and Deserialize method also has: MyObject obj = new MyObject (12, "some string ..."); XmlSerializer formatter = new XmlSerializer (typeof (MyObject)); Stream stream = new FileStream ( "MyFile.xml", FileMode .Create, fileaccess.write, Formatter.Serialize (stream, obj); // below is the anti-sequence-of-sequence stream.seek (0, seekorigin.begin) MyObject obj_out = (myObject) Formatter.deSerialize (stream) Stream.close (); console.writeline (obj_out); This simple list can be extended to utilize more XMLSerializer features, including the use of attribute control XML tags, using XML mode and SOAP encoding. Custom Series If you want users to realize serialization on classes, the organization of data streams is not fully satisfied, and can be defined from the defined serialization process by implementing the iSerializable interface on the object. This feature is especially useful when the value of the member variables after the deserialization, but needs to provide a value for the variable to rebuild the full state of the object. In addition to the implementation of the classes to be SERIALIZABLE, you have to implement the iSerializable interface, you need to implement the getObjectData method and a special constructor, and use this constructor when the object is reversed. When implementing the getObjectData method, the most frequently called SerializationInfo method is AddValue, which has overloaded version of all standard types (int, char, etc.); and the streamingContext parameter describes the source and target of a given serialization. This way we can know that we are serializing the object to persistence storage or serialize them across processes or machines. When it is reverse selecinstened, we call a set of GetXXxx methods provided by SerializationInfo, and they perform inverse operations of various AddValue overload versions for all standard type data. The following example shows how to implement iSerializable on the MyObject class mentioned in the previous part.
[Serializable] public class MyObject: ISerializable {public n1 int; public int n2; public String str; public MyObject () {} protected MyObject (SerializationInfo info, StreamingContext context) {n1 = info.GetInt32 ( "i"); n2 = Info.get32 ("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 you call GetObjectData during serialization, you need to fill the SerializationInfo object provided in the method call. Simply add a variable that will be serialized in the form of name / value. Its name can be any text. As long as the serialized data is sufficient to restore the object during the reverse sequence, it is possible to freely select the member variable added to the SerializationInfo. If the base object implements iSerializable, the derived class should call the GetObjectData method for its base object. It is important to emphasize that when adding iSerializable to a class, you need to implement GetObjectData and special-specific constructor with specific prototypes - it is important that the parameter list of this constructor must be the same as getObjectData, this constructor will be Use in the reverse sequence of sequences: The formatter is deserialized from the stream, and then the object is subjected to the object through this constructor. If you lack getObjectData, the compiler will issue a warning. However, since the constructor cannot be enforced, a warning will not be issued when the constructor is missing. If you try to deactivate a certain class without constructuring, an exception will occur. The current design is better than the SetObjectData method in eliminating potential security and version control issues. For example, if the setObjectData method is defined as part of an interface, this method must be a public method, which makes the user have to write code to prevent multiple calling setObjectData methods. You can imagine that if an object is performing some operations, and a malicious application calls the SETOBJECTDATA method for this object, it will cause some potential trouble. During the reverse selecente process, the constructor provided for this purpose is passed to the class. When an object is reverse selecinstened, any visibility constraint for the constructor will be ignored, so the class is marked as public, protected, internal, or private. A nice way is to mark the constructor as protecty in the case where the class is not encapsulated. If class is packaged, it should be marked as private. To restore the status of the object, simply use the name when serialization, retrieve the value of the variable from the SerializationInfo. If the base class implements iSerializable, the constructor of the base class should be called so that the base object can restore its variables.