5,117,952 members and growing! (12,616 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Advanced License: The Code Project Open License (CPOL)

Serialization for Rapid Application Development: A Better Approach

By Daniel Gidman

Serialize and Deserialize your objects with ease and simplicity
C#, .NET (.NET 2.0, .NET 3.0, .NET 3.5, .NET), Dev

Posted: 25 Apr 2008
Updated: 25 Apr 2008
Views: 2,407
Announcements



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 3.22 Rating: 4.60 out of 5
0 votes, 0.0%
1
1 vote, 20.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
4 votes, 80.0%
5

Introduction

A common problem that developers have with serialization is dealing with the unknown derived classes of the serialized types. Often serialization errors occur when Typed object collections have instances of an object derived Type. This is due to the XmlSerializer not knowing how to resolve the type of the object.

A common workaround is to register those derived types using the [XmlInclude(Type type)] custom attribute. While this works, it does not support future extensibility. Any new derived classes are not elegantly or easily handled. Custom serializers would have to be written on a case by case basis.

This is unacceptable for supporting rapid development. To solve the issue of supporting elegant and rapid development, I have created a simple service to make use of the XmlSerializer constructors with the inclusion of Type[] extraTypes and custom attributes that can be adorned to class declarations.

The solution presented is easy to use, elegant and requires a minimum of developer intervention to implement and make use of.

Using the Code

The use of this code is very simple and requires two things. The first requirement is that you adorn your class declarations with the custom attribute SerializableExtraType. The second requirement is that when you do your normal serialization and deserialization, you pass in the extraTypes that the SerializableExtraTypes is tracking. Static serialize and deserialize methods are part of the class.

Adorn a class with the required attributes like this:

[Serializable]
// example of registering class and all derived classes
[SerializableExtraType(typeof(Foo))]
// example of registering class with an unrelated class
[SerializableExtraType(typeof(SomethingElse))]
// example of registering with multiple classes in same attribute
[SerializableExtraType(new Type[] { typeof(ClassOne), typeof(ClassTwo) })]
public class Foo { public Foo() {} }

As with all serialization in .NET, all classes to be serialized also require the [Serializable] attribute. Here we simply add an additional attribute of [SerializableExtraType(Type type)]. Any standard serialization rules still apply. You do not need to adorn any derived classes of Foo as it will be inherited in all derived classes. This means you could add the attribute to an abstract base class and all your classes would be registered. I have given an example here where I register Foo for itself as well as for several other Types. This shows all possible variations of using the [SerializableExtraType] attribute.

Here is an example of serializing and deserializing an object that contains a collection of Foo and Foo derived objects:

string s = SerializableExtraTypes.Serialize(someObject, typeof(Foo));
object o = SerializableExtraTypes.Deserialize
            (strSomeObject, typeof(SomeObject), typeof(Foo));

That's it. It's very simple code and easy to use but the concept is wonderful in its simplicity and elegance. This in effect is like attaching [XmlInclude] from the opposite direction.

History

  • 25th April, 2008: Initial post

This is the first publication of this method. Please send any comments or suggestions on how to improve it. You can email me at danatcofo@gmail.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Daniel Gidman



Occupation: Team Leader
Company: The Triple-I Corporation
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionSample?memberMr.PoorEnglish4:56 26 Apr '08  
GeneralRe: Sample? [modified]memberDaniel Gidman10:34 26 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Apr 2008
Editor: Deeksha Shenoy
Copyright 2008 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project