C # serialization and deserialization

Source: Internet
Author: User

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:


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.