SunQuest
 
       .NET
  Home arrow .NET arrow Page 4 - Serialization with .NET
Iron Speed
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 
VeriSign Whitepapers 
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

Serialization with .NET
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2007-08-14

    Table of Contents:
  • Serialization with .NET
  • Serializing Custom Types
  • Serializing Only Some Internal Data
  • Other Attributes

  • 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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Serialization with .NET - Other Attributes


    (Page 4 of 4 )

    Besides the OnDeserializing attribute, .NET contains other similar attributes. When one of these attributes is applied to a method, then the method is called in the appropriate stage of serialization or deserialization. Here's a simple class that makes use of this family of attributes:

    using System;

    using System.Runtime.Serialization;

     

    [Serializable]

    public class AttributeTest

    {

       [OnSerializing]

       internal void OnSerializing(StreamingContext context)

       {

           Console.WriteLine("Serializing.");

       }

       

       [OnSerialized]

       internal void OnSerialized(StreamingContext context)

       {

           Console.WriteLine("Serialized.");

       }

       

       [OnDeserializing]

       internal void OnDeserializing(StreamingContext context)

       {

           Console.WriteLine("Deserializing.");

       }

       

       [OnDeserialized]

       internal void OnDeserialized(StreamingContext context)

       {

           Console.WriteLine("Deserialized.");

       }

    }

    Notice how all the methods look like the OnDeserializing method in our previous example in their return type and parameter. The attribute names should make it pretty easy to tell when each method is called. Nonetheless, let's see the serialization and deserialization of an instance of our type. However, instead of using a FileStream and wasting a file, let's use a MemoryStream:

    using System;

    using System.IO;

    using System.Runtime.Serialization.Formatters.Binary;

     

    class AttributeDemo

    {

       public static void Main(string[] args)

       {

           AttributeTest test = new AttributeTest();

           

           // Serialize it

           MemoryStream myStream = new MemoryStream();

           BinaryFormatter formatter = new BinaryFormatter();

           formatter.Serialize(myStream, test);

           

           // Deserialize it

           myStream.Position = 0;

           test = (AttributeTest)formatter.Deserialize(myStream);

           myStream.Close();

       }

    }

     

    Serializing.

    Serialized.

    Deserializing.

    Deserialized.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello.This article introduces some of the basics of serialization, covering...
       · Ease to understand and definitely good article
     

    .NET ARTICLES

    - 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 ...
    - Delving Deeper into Serialization with .NET
    - Serialization with .NET

    Iron Speed




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