.NET
  Home arrow .NET arrow Page 2 - 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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Delving Deeper into Serialization with .NET - A Caution on Serialization and Inheritance


    (Page 2 of 4 )

    Now we have a serializable Shape class, but does the fact that Shape is serializable make ThreeDimensionalShape serializable as well? In other words, does a class inherit the ability to be serialized? To find out, let's write and run some code that attempts to serialize instances of both classes:

    using System;

    using System.IO;

    using System.Runtime.Serialization.Formatters.Binary;

     

    class InheritanceAndSerialization

    {

       public static void Main(string[] args)

       {

           // Create a ThreeDimensionalShape

           ThreeDimensionalShape cylinder = new ThreeDimensionalShape(12.57, 3.14);

           

           // Serialize cylinder

           MemoryStream myStream = new MemoryStream();

           BinaryFormatter formatter = new BinaryFormatter();

           formatter.Serialize(myStream, cylinder);

           myStream.Close();

           Console.WriteLine("Cylinder serialized.");

       }

    }

    When we run the above code, we get a nasty error:

    Unhandled Exception: System.Runtime.Serialization.SerializationException:Type ThreeDimensionalShape is not marked as Serializable.

    ...

    So, no, a class that wants to be serialized must apply the Serializable attribute, even if its base class applies the attribute. If we modify the ThreeDimensionalShape class by applying the attribute, then the above code runs fine:

    [Serializable]

    public class ThreeDimensionalShape : Shape

    {

       ...

    }

    Cylinder serialized.

    Note that ThreeDimensionalShape does not have access to Shape's area field since we have it set as private. However, when a ThreeDimensionalShape is serialized, area is serialized too, and when the object is deserialized, all of the serializable internal data is restored. .NET handles all of this for us, so properly serializing child class instances is as simple as marking the child class as Serializable. .NET handles all of the work behind the scenes.

    There is still one situation we haven't looked at, though: can a child class be serialized if its parent class isn't marked with the Serializable attribute? Remove the Serializable attribute from Shape and run the above code again:

    Unhandled Exception: System.Runtime.Serialization.SerializationException:Type Shape in assembly _Serialization, Version=0.0.0.0, Culture=neutral is not marked as serializable.

    ...

    No, it would appear that it's not possible to serialize an instance of a class whose parent class isn't serializable, even if the child class is itself serializable. .NET's serialization mechanisms work their magic only if we play by the rules, and the rules state that in order for instances of a class to be serializable, its base class must also be serializable. That said, however, it is still possible to serialize an instance of such a class, though the solution isn't pretty since .NET won't automatically do the work for us.

    More .NET Articles
    More By Peyton McCullough


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

    .NET ARTICLES

    - 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
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway