. NET implementation of serialization and deserialization instance resolution

Source: Internet
Author: User

Serialization and deserialization are common applications in. NET programming, and this article shows how. NET implements serialization and deserialization in an instance. Specific as follows:

In general, the. Net

Serialization is essentially saving all the relevant data of an object as a binary file

(Note: is an object)


And all types associated with this object must be serializable, so add the [Serializable] attribute to the related class

Object types include: the type that the object itself contains, the parent class


After owning the desired object:

1. Convert an object to binary data using a dedicated image conversion BinaryFormatter

2. Writing binary data to a file Filesteam


Deserialization is the conversion of a binary file into an object

The sample code looks like this:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.io;using System.runtime.serialization.formatters.binary;namespace consoleapplication1{class Program {s      tatic void Main (string[] args) {person per;//the object that will be serialized Console.WriteLine ("------Serialization and deserialization------");      Console.WriteLine ("Whether to read the serialized object per");      String str = Console.ReadLine (); if (str = = "Yes") {if (!          File.exists ("Save.bin")) {Console.WriteLine ("You have not serialized the per");        Return } using (FileStream fs = new FileStream ("Save.bin", FileMode.Open)) {BinaryFormatter BF = new Bina          Ryformatter (); per = BF. Deserialize (FS) as person;//converts the binary data to a per object.          Sayhi ();        Console.ReadLine ();        }} else {per = new person (); Per.        Name = "Xiao Li"; using (FileStream fs=new FileStream ("Save.bin", FileMode.Create)) {binaryformatter BF = new BinaryFormatter (); Bf.          Serialize (fs,per);//Convert the per object to binary data and save.          Console.WriteLine ("Serialization succeeds");        Console.ReadLine ();    }}}} [Serializable] class Person {public string Name;    public void Sayhi () {Console.WriteLine ("Hello {0}", Name); }  }}

It is believed that the example of this article is helpful for us to understand further the serialization and deserialization of. Net.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
. NET implementation of serialization and deserialization instance resolution

This address: http://www.paobuke.com/develop/c-develop/pbk23656.html






Related Content C # event Instance details C # for Loop classic case Collection C # instance code of the lucky draw upgrade can be imported into the database through tabular data, lottery settings, pumping C # Programming and Visual Studio use Tips (bottom)
C # Image rotation and rollover (rotatefliptype) Usage Analysis C # implementation percent to Decimal method C # How to upload and save a picture using a socket C # show all picture files in a folder

. NET implementation of serialization and deserialization instance resolution

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.