SerializableAttribute in C #

Source: Internet
Author: User

usingUnityengine;usingSystem.Collections;usingSystem.IO;usingSystem.Runtime.Serialization.Formatters.Binary;//A test object that needs to is serialized.[system.serializable] Public classTestsimpleobject { Public intMember1;  Public stringMember2;  Public stringMember3;  Public DoubleMember4; //A Field that's not serialized.[System.nonserialized] Public stringMember5;  PublicTestsimpleobject () {member1= One; Member2="Hello"; Member3="Hello"; Member4=3.14159265; Member5="Hello world!"; }             Public voidPrint () {Debug.Log ("member1 ="+member1); Debug.Log ("member2 ="+member2); Debug.Log ("Member3 ="+Member3); Debug.Log ("member4 ="+member4); Debug.Log ("Member5 ="+member5); }}[executeineditmode] Public classTestserializerable:monobehaviour {//Use this for initialization    voidStart () {testserializerable.main (); }     Public Static voidMain () {//creates a new Testsimpleobject object.Testsimpleobject obj =NewTestsimpleobject (); Debug.Log ("before serialization The object contains:"); Obj.                        Print (); //Opens a file and serializes the object into it in binary format.Stream stream = File.Open ("Data.xml", FileMode.Create); //SoapFormatter formatter = new SoapFormatter ();BinaryFormatter Formatter=NewBinaryFormatter (); Formatter.            Serialize (stream, obj); Stream.                        Close (); //empties obj.obj =NULL; //Opens file "Data.xml" and deserializes the object from it.stream = File.Open ("Data.xml", FileMode.Open); //formatter = new SoapFormatter ();Formatter=NewBinaryFormatter (); Obj=(Testsimpleobject) formatter.            Deserialize (stream); Stream.                        Close (); Debug.Log ("After deserialization The object contains:"); Obj.    Print (); }}

The above code runs in the Unity environment

SerializableAttribute in C #

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.