.NET
  Home arrow .NET arrow Page 3 - Delving Deeper into Serialization with .NE...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Delving Deeper into Serialization with .NET
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-08-15

    Table of Contents:
  • Delving Deeper into Serialization with .NET
  • A Caution on Serialization and Inheritance
  • ISerializable
  • Deserialization

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Delving Deeper into Serialization with .NET - ISerializable


    (Page 3 of 4 )

    So far, the serialization process has been mostly handled for us. All that we've done is apply attributes where necessary, and .NET has figured out how to do everything on its own. Where we've needed to intervene and participate in the serialization and deserialization processes, we've created methods that are called at the proper time. This automation is generally a good thing. However, it is possible to gain more direct control over the processes of serialization and deserialization. This can be done by implementing the ISerializable interface.

    Recall from the previous part how we needed more control over the User class. The class defined a DateTime called sessionStartTime that was marked with the NonSerialized attribute. Since the value of this member isn't stored, we needed to give it an appropriate value upon deserialization. This was accomplished through the OnDeserializing attribute and a simple method. The method, called during the deserialization process, set sessionStartTime to the current date and time. This got the job done, but we could also have implemented ISerializable. Let's revisit the example and reconstruct User to implement ISerializable. Here is the original User class, without the OnDeserializing fix:

    using System;

     

    [Serializable]

    public class User

    {

       private string name;

       private DateTime sessionStartTime;

       

       public User(string name)

       {

           this.name = name;

           sessionStartTime = DateTime.Now;

       }

       

       public string Name

       {

           get

           {

              return name;

           }

       }

       public DateTime SessionStartTime

       {

           get

           {

              return sessionStartTime;

           }

       }

    }

    Before, because we did not want sessionStartTime to be serialized, we applied the NonSerialized attribute, which prevented .NET from storing the field's value. Now, however, we don't have to apply the attribute. The ISerializable interface defines a method called GetObjectData. In this method, we specify exactly what is to be serialized and how. Only what we specify to be serialized is serialized; everything else is not. Here are the changes required for User to implement the ISerializable interface:

    ...

    using System.Runtime.Serialization;

     

    [Serializable]

    public class User : ISerializable

    {

       ...

       public virtual void GetObjectData(SerializationInfo info, StreamingContext context)

       {

           info.AddValue("name", name, typeof(string));

       }

    }

    Notice that GetObjectData has no return type and accepts two parameters: a SerializationInfo and a StreamingContext. The former is what's important here. It contains (or needs to contain, rather) information about the members to be serialized. To add data, the AddValue method is called, which accepts the name of the data, the value of the data and the type of the data. Here, we specify that the name field is to be serialized. Also, it's important that the method be virtual, as we'll see later.

    More .NET Articles
    More By Peyton McCullough


       · Hello, all!In Serialization with .NET, I explained how to use automatic...
     

    .NET ARTICLES

    - Introducing .NET
    - Building Business Objects for an Application
    - Methods for an Application`s Business Logic ...
    - Properties of an Application`s Business Logi...
    - Classes and Properties in an Application`s B...
    - Organizing Code for the Business Logic Layer...
    - Building the Business Logic Layer
    - Completing a Business Layer with Windows Wor...
    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek