(go + organize) C # BinaryFormatter for serialization and deserialization

Source: Internet
Author: User

serialization, which is also serialized, is a mechanism used by the. NET runtime environment to support the flow of user-defined types. The purpose is to persist the custom object in some form of storage, or to transfer the object from one place to another.

. The NET Framework provides two ways of serialization: 1, serialization using BinaryFormatter, 2, serialization with XmlSerializer.    The first provides a simple binary data stream along with some additional type information, and the second formats the data stream as an XML store. You can use the [Serializable] property to mark a class as serializable. If the elements of a class do not want to be serialized, 1, you can use the [NonSerialized] property to flag, 2, you can use [XmlIgnore] to flag.

Serialization means that the current state of an object is persisted, and that the state of an object is represented by a property in an object-oriented program, so when the class is serialized, it is saved from the property read value in some format, and the member function of the class is not serialized, and. NET has several default-provided serialization, binary serialization, XML and JSON serialization serialize all the instance common attributes.

The BinaryFormatter serializes and deserializes objects in binary format.

Binaryformatte Serialization: Converts an object into binary.

Binaryformatte deserialization: Converts the binary into an object.

namespaces: System.Runtime.Serialization.Formatters;

Two common methods:

Deserialize (stream): deserializes the specified stream into an object

Serialize (Stream,? Object): Serializes objects to a given stream

Two Common properties:

Serializable: Indicates that it can be serialized

Nonserializable: Masking is serialized

For example:

Using Unityengine;Using System;Using System.Collections;Using System.IO;Using System.Collections.Generic;Using System.Runtime.Serialization.Formatters.Binary; [Serializable]Indicates that the class can be serializedClassperson{PrivateString name; [NonSerialized]Indicates that the following age field is not serializedPrivateint age;PublicString name{get {return name;set {name =Value;}}Publicint age{get {return age;}set {age =Value;}}PublicPerson () { }PublicPerson (String name,int age) {THIS.name = name;This.age = age;}PublicvoidSayhi () {Debug.logformat ("I am {0}, this year {1} years old", name, age);}PublicClassBinaryserializer:Monobehaviour {String filePath = Directory.GetCurrentDirectory () +"/binaryfile.txt";Use this for initializationvoidStart () {list<person> listpers =New List<person> (); Person Per1 =New Person ("Zhang San",18); Person Per2 =New Person ("John Doe",Listpers.add (Per1); Listpers.add (PER2); Serializemethod (listpers);Serialization of Deserializemethod ();Deserialization Debug.Log ("Done!");}voidDeserializemethod ()Binary deserialization {FileStream fs =New FileStream (FilePath, FileMode.Open); BinaryFormatter BF =New BinaryFormatter (); list<person> list = bf. Deserialize (FS)as list<person>; if (list! = null) {for (int i = 0; i < list. Count; i++) {list [i]. Sayhi ();}} Fs. Close ();} void serializemethod (list<person> listpers) //binary serialization {FileStream fs = new FileStream (FilePath, FileMode.Create); BinaryFormatter bf = new BinaryFormatter (); Bf. Serialize (FS, listpers); fs. Close ();} //Update is called once per Frame void update () {}}       

After the serialized text is opened, the contents are as follows:

NonSerialized function: The marked fields are assigned empty

Deserialization output:

Hello everyone, I am Zhang San, 0 years old this year

Hello everyone, I am John Doe, 0 years old this year

As a result, the non-serialized field stores a value of NULL

Articles reproduced from: 52790131, 78478458

(go + organize) C # BinaryFormatter for serialization and deserialization

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.