12.16 Recreating Objects with Deserialization

 <  Day Day Up  >  

You want to construct a new object that has been previously serialized to a file.


Technique

The technique for deserialization is generally much easier than the initial steps for serialization because most of the steps are already finished. To deserialize an object, create the appropriate serialization formatter as discussed in the previous section. Note that you must use the same serialization object for deserialization, meaning you can't serialize an object with a BinaryFormatter and deserialize it with an XmlSerializer . Once the object is created, open a file stream for reading and call the Deserialize method, passing the stream object as the only parameter. This method returns an Object type that you must cast to the data type of the object being deserialized. The following code shows the deserialization of an object using each serialization method:

 
 private void mnuLoadBinary_Click(object sender, System.EventArgs e) {     openFileDialog1.FilterIndex = 1;     if( openFileDialog1.ShowDialog() != DialogResult.OK )         return;     IFormatter formatter = new BinaryFormatter();     Stream stream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess graphics/ccc.gif .Read, FileShare.None);     mObject = (SerializedObject) formatter.Deserialize(stream);     stream.Close();     pgObject.SelectedObject = mObject; } private void mnuLoadXML_Click(object sender, System.EventArgs e) {     openFileDialog1.FilterIndex = 2;     if( openFileDialog1.ShowDialog() != DialogResult.OK )         return;     XmlSerializer formatter = new XmlSerializer( mObject.GetType() );     Stream stream = new FileStream(openFileDialog1.FileName, FileMode.Open,         FileAccess.Read, FileShare.None);     mObject = (SerializedObject) formatter.Deserialize( stream );     stream.Close();     pgObject.SelectedObject = mObject; } private void mnuLoadSoap_Click(object sender, System.EventArgs e) {     openFileDialog1.FilterIndex = 3;     if( openFileDialog1.ShowDialog() != DialogResult.OK )         return;     IFormatter formatter = new SoapFormatter();     Stream stream = new FileStream(openFileDialog1.FileName, FileMode.Open,         FileAccess.Read, FileShare.None);     mObject = (SerializedObject) formatter.Deserialize(stream);     stream.Close();     pgObject.SelectedObject = mObject; } 

Comments

One thing you must take into consideration when deserializing an object is that no initialization code is run when an object is successfully created through the deserialization process. In other words, the constructor for an object is not called, which means you might have to perform additional initialization for internal data that might need initialization. Although the data members might be successfully recreated, some data might rely on external factors to be valid. Take, for example, a private member variable that contains the name of the machine the application is being run on. If the object is serialized on one machine and then deserialized on another, then the internal data member becomes invalid and must be reinitialized somehow.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net