.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 4
    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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT