650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194P02B3-0.gif "/> my General explained the object to me so that the object is what the subject is currently concerned. People always have a habit, especially girls. They like to collect things, such as jewelry, so, and bags. We hope to save all the good things permanently. In fact, in the process of designing and implementing the Program, the object is also very friendly, but it is destroyed every time you press Ctrl + F5, we can also make the object persistent for a longer time. Persistence refers to the storage of objects in some media. Objects in the memory are stored in files or streams. Objects in the object class must be persistently stored. There are three methods for reading and writing files: file stream reading and writing, XML file reading and writing, and serialization and deserialization. File stream reading and writing generally uses the ReadLine () and WriteLine () methods in StreamReader and StreamWriter for reading and writing directly. The XML document mainly uses the Load method in the document to Load, XmlNode, XmlDeclaration, and other object methods in the class. Finally, we can use a sequence to read and write files. The specific steps are as follows:
Serialization Process
1. Set the object class to serializable.
2. Create an object class object.
3. Create a serialization Tool Object.
4. File streams should be written)
5. serialization.
6. Clear resources during serialization.
Deserialization Process
1. Create a serialization Tool Object.
2. Create a file stream and read it)
3. deserialization.
4. Clear resources during serialization.
BinaryFormatter
The content serialized in binary serialization mode can only be read and written on the. net platform.
Xml serialization can share data among multiple technical platforms.
The Xml serialization method requires that the class must be a public class.
The following is a simple example:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194P024H-1.png "title =" Capture. PNG "/>
This is the first step: Set the object class to serializable
Card card = new Card (); // instantiate an object card. name = "aha"; card. phone = "13391143820"; card. qq = "145282812"; card. e-mail = "145282812@163.com"; card. address = "Shaanxi yongshou"; BinaryFormatter bf = new BinaryFormatter (); // create the serialization Tool Object FileStream fs = new FileStream ("test. dat ", FileMode. create); // The file stream should be written to) bf. serialize (fs, card); // serialization. Fs. Flush (); fs. Close ();
This is the whole serialization process.
BinaryFormatter bf = new BinaryFormatter (); // create the serialization Tool Object FileStream fs = new FileStream ("test. dat ", FileMode. open); // create a read Stream Object obj = bf. deserialize (fs); // deserialization // List <Card> list = (List <Card>) obj; List <Card> list = obj as List <Card>; // convert obj to the Card object foreach (Card x in list) {Console. writeLine (x);} fs. close ();
This is the deserialization process. This method is very convenient for reading and writing files. It is recommended.
P.S: as is used for type conversion before as is the onject type, as is followed by the reference type)
Is used to determine whether an object is of a certain type
Typeof is used to obtain the Type object of a class of an object.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194P05913-2.gif "/>Ajax Girl, come on! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194P05913-2.gif "/>
This article is from the "Ajax girl" blog!