Object Serialization

Demonstrates how to serialize and deserialize an object.

 
 
 
 
 
(0)
1,839 times
Add to favorites
5/25/2011
E-mail Twitter del.icio.us Digg Facebook
<!--TO READ THIS HELP FILE, RIGHT-CLICK ON THE FILE NAME IN THE
    SOLUTION EXPLORER PANE AND SELECT "VIEW IN BROWSER"-->

<html><head><title>Serialization Sample</title></head><body><h1>Serialization Sample</h1><div id="mainSection"><div id="mainBody"><div id="allHistory" class="saveHistory" onsave="saveAll()" onload="loadAll()"><font color="DarkGray"></font><p /></div><div class="introduction"><p>To get samples and instructions for installing them, see the following:</p><ul><li><p>Click <span class="ui">Samples</span> on the Visual Studio <span class="ui">Help</span> menu.</p><p>For more information, see <mshelp:link keywords="e121a9c2-b35b-4b3f-8cba-30edd84c835f" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">Locating Sample Files</mshelp:link>.</p></li><li><p>The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site. </p></li><li><p>You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.</p><div class="alert"><table width="100%"    IS" without warranties.</p></td></tr></table></div></li></ul></div><h3 class="procedureSubHeading">To run this sample</h3><div class="subSection"><ul><li><p>Press F5.</p></li></ul></div><h1 class="heading"><span   style="cursor:default;"   tabindex="0">Demonstrates</span></h1><div id="demonstratesSection" class="section" name="collapseableSection" style=""><p>This application contains two classes, one with standard serialization and one with custom serialization. The user interface includes buttons to serialize and deserialize the two classes. The class with standard serialization can be serialized to either a SOAP file or a binary file. The class with custom serialization is serialized to a SOAP file. A <mshelp:link keywords="T:System.Windows.Forms.TextBox" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">TextBox</mshelp:link> control is used to display the SOAP files. The two classes both have the <mshelp:link keywords="T:System.SerializableAttribute" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">SerializableAttribute</mshelp:link> attribute. The classes are described in the following table.</p><div class="tableSection"><table width="50%" cellspacing="2" cellpadding="5" frame="lhs"><tr><th><p>Class Member</p></th><th><p>SerializationClass</p></th><th><p>CustomSerializationClass</p></th></tr><tr><td><p><span class="code">PublicVariable</span></p></td><td><p>The public variable is serialized and deserialized without change.</p></td><td><p>The public variable is serialized and deserialized without change.</p></td></tr><tr><td><p><span class="code">PublicProperty</span></p></td><td><p>The private variable that supports the property is serialized and deserialized without change.</p></td><td><p>The private variable that supports the property is serialized as -1, regardless of the value set in the user interface. It is therefore deserialized as -1.</p></td></tr><tr><td><p><span class="code">NonSerializedVariable</span></p></td><td><p>This variable is not serialized, because it is marked with the <mshelp:link keywords="T:System.NonSerializedAttribute" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">NonSerializedAttribute</mshelp:link>. The public variable is not serialized, and the deserialized value is 0.</p></td><td><p>Though the variable is marked by the <mshelp:link keywords="T:System.NonSerializedAttribute" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">NonSerializedAttribute</mshelp:link>, it is serialized, because the custom serialization overrides this setting. The public variable is serialized and deserialized without change.</p></td></tr></table></div><p>Three types of serialization are demonstrated:  </p><ul><li><p><span class="label">SOAP formatting</span>   <span class="code">Class1</span> is serialized using the <mshelp:link keywords="T:System.Runtime.Serialization.Formatters.Soap.SoapFormatter" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">SoapFormatter</mshelp:link> class.</p></li><li><p><span class="label">Binary formatting</span>   <span class="code">Class1</span> is serialized using <mshelp:link keywords="T:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">BinaryFormatter</mshelp:link> class.</p></li><li><p><span class="label">Custom formatting</span>   <span class="code">Class2</span> implements the <mshelp:link keywords="T:System.Runtime.Serialization.ISerializable" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">ISerializable</mshelp:link> interface, including the <mshelp:link keywords="M:System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">GetObjectData</mshelp:link> method to perform custom serialization.</p></li></ul></div></div><div id="footer"><div class="footerLine"></div><!--<include item="copyrightStatement"/>--><A NAME="feedback"></A><span id="fb" class="feedbackcss" /> 
             
                
               
                
                
               
               
               
              
                
               
              
               
               
               
         </div></div></body></html>