Sie sind auf Seite 1von 7

Serialization

Many modern OOP languages support serialization: the storing of the state of an object to a file for persistence or transport. .NET offers at least three different formats for serializing: binary, SOAP, and XML. Each have their pros and cons. None seem to give you everything you might want. Below are brief descriptions of the different formats and examples based on the ClickerCounter.

Binary serialization
The binary format stores the state of an object in (surprise!) a binary format.

Pros
The most efficient method. Private and public data can be stored.

Cons
Can only be deserialized into the same program, namespace, etc. that created it Cannot be hand edited after the file is made. Binary format may not be transportable through firewalls.

Example
// An example that shows how to use **binary** serialization files. using System; // Be sure to add using statements for System.IO, System.Runtime.Serialization, // and System.Runtime.Serialization.Formatters.Binary if you want to serialize. using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace ClickerConsole { // The next line marks the Clicker class as serializable: [Serializable] class Clicker { private int count; private int click_limit; // constructor public Clicker(int click_limit) { this.click_limit = click_limit; this.reset(); }

public Clicker() { this.click_limit = 100; this.reset(); } // accessor public int get_count() { return count; } // reset the count public void reset() { count = 0; } // click the counter public void click() { if (count < click_limit) count++; else reset(); } // Static method to serialize Clicker objects public static void serialize(Clicker the_clicker, string the_filename) { FileStream filestream = new FileStream(the_filename, FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); // Use the BinaryFormatter's Serialize method to make a serialzed // version of the_clicker using the file pointed to by filestream: bf.Serialize(filestream, the_clicker); filestream.Close(); } // Static method to deserialize Clicker objects public static Clicker deserialize(string the_filename) { FileStream filestream2 = new FileStream(the_filename, FileMode.Open); BinaryFormatter bf2 = new BinaryFormatter(); // Use the BinaryFormatter's Deserialize method to make an object // from the file and explicity cast it to the desired class. Clicker a_clicker = (Clicker)bf2.Deserialize(filestream2); filestream2.Close(); return a_clicker; } } // We use the Tester class to test various aspects of the code above. class Tester { static void Main() { // Make a clicker and use it. Clicker my_clicker = new Clicker(4); my_clicker.click(); my_clicker.click(); my_clicker.click(); Console.WriteLine(my_clicker.get_count()); // now serialize my_clicker: // // // // 1 2 3 should print 3

needed

string my_filename = @"C:\temp\clicker.dat"; Clicker.serialize(my_clicker, my_filename);

// change this as

// and make a new Clicker from the serialized file we just created: Clicker your_clicker = Clicker.deserialize(my_filename); Console.WriteLine(your_clicker.get_count()); your_clicker.click(); your_clicker.click(); Console.WriteLine(your_clicker.get_count()); Console.WriteLine(my_clicker.get_count()); } } } // // // // // should print 3 4 0 should print 0 should print 3

SOAP serialization
The SOAP format stores the state of an object in Simple Object Access Protocol formatan XML-based format that is a standard in exchanging structured information on the Web and in computer networks.

Pros
Since SOAP is based on XML, it should be easily transportable through firewalls. Private and public data can be stored. Can be hand edited after the file is made.

Cons
Can only be deserialized into the same program, namespace, etc. that created it.

Example
To use the SOAP formatter, you will need to manually add the reference to System.Runtime.Serialization.Formatters.Soap to your project using the Solution Explorer.
// An example that shows how to use **SOAP** serialization files. using System; // Be sure to add using statements for System.IO, System.Runtime.Serialization, // and System.Runtime.Serialization.Formatters.Soap if you want to serialize. using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap; // Note: you may have add the reference to System.Runtime.Serialization.Formatters.Soap // using the Solution Explorer. namespace ClickerConsoleSoap { // The next line marks the Clicker class as serializable:

[Serializable] public class Clicker { private int count; private int click_limit; // constructor public Clicker(int click_limit) { this.click_limit = click_limit; this.reset(); } public Clicker() { this.click_limit = 100; this.reset(); } // accessor public int get_count() { return count; } // reset the count public void reset() { count = 0; } // click the counter public void click() { if (count < click_limit) count++; else reset(); } // Static method to serialize Clicker objects public static void serialize(Clicker the_clicker, string the_filename) { FileStream filestream = new FileStream(the_filename, FileMode.Create); SoapFormatter sf = new SoapFormatter(); // Use the SoapFormatter's Serialize method to make a serialzed // version of the_clicker using the file pointed to by filestream: sf.Serialize(filestream, the_clicker); filestream.Close(); } // Static method to deserialize Clicker objects public static Clicker deserialize(string the_filename) { FileStream filestream2 = new FileStream(the_filename, FileMode.Open); SoapFormatter sf2 = new SoapFormatter(); // Use the SoapFormatter's Deserialize method to make an object // from the file and explicity cast it to the desired class. Clicker a_clicker = (Clicker)sf2.Deserialize(filestream2); filestream2.Close(); return a_clicker; } } // We use the Tester class to test various aspects of the code above. public class Tester

static void Main() { // Make a clicker and use it. Clicker my_clicker = new Clicker(4); my_clicker.click(); my_clicker.click(); my_clicker.click(); Console.WriteLine(my_clicker.get_count()); // now serialize my_clicker: string my_filename = @"C:\temp\clicker.soap"; Clicker.serialize(my_clicker, my_filename); // and make a new Clicker from the serialized file we just created: Clicker your_clicker = Clicker.deserialize(my_filename); Console.WriteLine(your_clicker.get_count()); your_clicker.click(); your_clicker.click(); Console.WriteLine(your_clicker.get_count()); Console.WriteLine(my_clicker.get_count()); // // // // // should print 3 4 0 should print 0 should print 3 // // // // 1 2 3 should print 3

needed

// change this as

} }

XML serialization
The XML format stores the state of an object in a simple XML-based format.

Pros
It should be easily transportable through firewalls. Can be deserialized into any program, namespace, etc. Can be hand edited after the file is made.

Cons
Can only store public data.

Example
Since private data cannot be serialized using the XML formatter, I have made Properties of the state variables. It's not enough that the Property be public, it must also be both readable and writiable.
// An example that shows how to use **XML** serialization files. using System; using System.IO; using System.Xml.Serialization; namespace ClickerConsoleXML {

public class Clicker { // constructors public Clicker(int click_limit) { this.click_limit = click_limit; this.reset(); } public Clicker() { this.click_limit = 100; this.reset(); } // instance variables and properties private int count; private int click_limit; public int Count { get { return count; } set { count = (value < click_limit ? value : click_limit); } } public int ClickLimit { get { return click_limit; } set { click_limit = value; } } // reset the count public void reset() { count = 0; } // click the counter public void click() { if (count < click_limit) count++; else reset(); } // Static method to serialize Clicker objects public static void serialize(Clicker the_clicker, string the_filename) { // Create a new XmlSerializer instance with the type of the class XmlSerializer serializer = new XmlSerializer(typeof(Clicker)); // Create a new file stream to write the serialized object to a file TextWriter my_stream = new StreamWriter(the_filename); // Save the object with the Serialize method serializer.Serialize(my_stream, the_clicker); // Cleanup my_stream.Close(); } public static Clicker deserialize(string the_filename) {

// Create a new XmlSerializer instance with the type of the class XmlSerializer serializer = new XmlSerializer(typeof(Clicker)); // Create a new file stream for reading the XML file FileStream ReadFileStream = new FileStream(the_filename, FileMode.Open, FileAccess.Read, FileShare.Read); // Load the object saved above with the Deserialize method Clicker a_clicker = (Clicker)serializer.Deserialize(ReadFileStream); // Cleanup ReadFileStream.Close(); } } // We use the Tester class to test various aspects of the code above. public class Tester { static void Main() { // Make a clicker and use it. Clicker my_clicker = new Clicker(4); my_clicker.click(); my_clicker.click(); my_clicker.click(); Console.WriteLine(my_clicker.Count); // now serialize my_clicker: string my_filename = @"C:\temp\clicker.xml"; Clicker.serialize(my_clicker, my_filename); // and make a new Clicker from the serialized file we just created: Clicker your_clicker = Clicker.deserialize(my_filename); Console.WriteLine(your_clicker.Count); your_clicker.click(); your_clicker.click(); Console.WriteLine(your_clicker.Count); Console.WriteLine(my_clicker.Count); } } } // // // // // should print 3 4 0 should print 0 should print 3 // // // // 1 2 3 should print 3 return a_clicker;

needed

// change this as

Das könnte Ihnen auch gefallen