Someone in the group just happened to ask this question, but it was not mentioned in the previous blog. I plan to get a blog to briefly mention this knowledge point.
The concepts of serialization and deserialization are mentioned in the MSDN document.
Serialization: the process of converting the object state to the form of persistence or transmission.
Deserialization: it is a deserialization inverse process, that is, the process of converting a stream into an object.
Both processes ensure that data is easy to transmit and store.
For more information, see http://msdn.microsoft.com/zh-cn/library/7ay27kt9 (v = vs.100). aspx.
The complete code below demonstrates how to serialize an object and deserialize (restore an object.
Namespace ConsoleApplication1 {class Program {static void Main (string [] args) {Object student = new Student () {StudentID = "007", StudentName = "guwei4037 "}; string result = ObjectToString <Object> (student); Console. writeLine (result + "\ r \ n"); Student newResult = StringToObject <Student> (result); Console. writeLine ("ID: {0}, Name: {1}", newResult. studentID, newResult. studentName);} // <summary> // convert the object to a string (serialized to a Base64 encoded string) /// </summary> /// <param name = "obj"> Object </param> /// <returns> string </returns> public static string ObjectToString <T> (T obj) {using (MemoryStream stream = new MemoryStream () {IFormatter formatter = new BinaryFormatter (); formatter. serialize (stream, obj); stream. position = 0; byte [] buffer = new byte [stream. length]; stream. read (buffer, 0, buffer. length); return Convert. toBase64String (buffer) ;}/// <summary> // convert a string to an object (deserialization of a Base64 encoded string into an object) /// </summary> /// <param name = "str"> string </param> /// <returns> Object </returns> public static T StringToObject <T> (string str) {using (MemoryStream stream = new MemoryStream () {byte [] bytes = Convert. fromBase64String (str); stream. write (bytes, 0, bytes. length); stream. position = 0; IFormatter formatter = new BinaryFormatter (); return (T) formatter. deserialize (stream) ;}}/// <summary> /// serializable class, use Serializable to mark this class Serializable // </summary> [Serializable] public class Student {public string StudentID {get; set ;}public string StudentName {get; set ;}}}
Running result: