Serialization and deserialization of C # classes

Source: Internet
Author: User

Serialization (serialization) transforms the state information of an object into a process that can be stored or transmitted in a form. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing the state of the object from the store. (Excerpt from Baidu Encyclopedia)

In the process of many communication or data storage, the process of serialization and deserialization is required.

In C #, if you want to serialize a custom class, simply add the serializable tag when you define the class. Such as:

123456 [Serializable]publicclass Person{    public string name;    public intage;}

There are two common ways to serialize: binary and XML, are divided into System.Runtime.Serialization.Formatters.Binary.BinaryFormatter and System.Xml.Serialization.XmlSerializer implementations.

BinaryFormatter does not care about the actual type when it is serialized and deserialized. But XmlSerializer need.

  

A simple serialization helper class that you write yourself

  

    PublicStaticClassSerializehelper {///<summary>///Convert a byte array to a string using UTF8 encoding///</summary>///<param name= "Data" ></param>///<returns></returns>PublicStaticString ConvertToString (Byte[] data) {return Encoding.UTF8.GetString (data,0, data. Length); }///<summary>///Converts a byte array into a string using the specified character encoding///</summary>///<param name= "Data" ></param>///<param name= "Encoding" ></param>///<returns></returns>PublicStaticString ConvertToString (Byte[] data, Encoding Encoding) {return encoding. GetString (data,0, data. Length); }///<summary>///To convert a string to a byte array using UTF8 encoding///</summary>///<param name= "str" ></param>///<returns></returns>PublicStaticByte[] Converttobyte (StringSTR) {ReturnEncoding.UTF8.GetBytes (str); }///<summary>///Converts a string to a byte array using the specified character encoding///</summary>///<param name= "str" ></param>///<param name= "Encoding" ></param>///<returns></returns>PublicStaticByte[] Converttobyte (StringSTR, Encoding Encoding) {ReturnEncoding. GetBytes (str); }///<summary>///Serializing an object into binary data///</summary>///<param name= "obj" ></param>///<returns></returns>PublicStaticByte[] Serializetobinary (Objectobj) {MemoryStream stream =NewMemoryStream (); BinaryFormatter BF =NewBinaryFormatter (); Bf. Serialize (stream, obj);byte[] data =Stream. ToArray (); Stream. Close ();ReturnData }///<summary>///Serializing objects to XML data///</summary>///<param name= "obj" ></param>///<returns></returns>PublicStaticByte[] Serializetoxml (Objectobj) {MemoryStream stream =NewMemoryStream (); XmlSerializer xs =NewXmlSerializer (obj. GetType ()); Xs. Serialize (stream, obj);byte[] data =Stream. ToArray (); Stream. Close ();ReturnData }///<summary>///Deserialization of binary data///</summary>///<param name= "Data" ></param>///<returns></returns>PublicStaticObject Deserializewithbinary (Byte[] data) {MemoryStream stream =NewMemoryStream (); Stream. Write (data,0, data. Length); Stream. Position =0; BinaryFormatter BF =NewBinaryFormatter ();Object obj =Bf. Deserialize (stream); Stream. Close ();ReturnObj }///<summary>///Deserializes binary data into a specified type object///</summary>///<typeparam name= "T" ></typeparam>///<param name= "Data" ></param>///<returns></returns>PublicStatic T deserializewithbinary<t> (Byte[] data) {ReturnT Deserializewithbinary (data); }///<summary>///Deserializes XML data into a specified type object///</summary>///<typeparam name= "T" ></typeparam>///<param name= "Data" ></param>///<returns></returns> public static T deserializewithxml<t> (byte [] data) {MemoryStream stream = new MemoryStream (); stream. Write (data, 00; XmlSerializer xs = new XmlSerializer (typeof (T)); object obj = XS. Deserialize (stream); Stream. Close (); return (T) obj;}       

Note that when deserializing, byte[] is written to the stream, the cursor needs to be moved to the first place and the position zeroed out, otherwise the deserialization will error.

Serialization and deserialization of C # classes

Related Article

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.