Click here to Skip to main content
14,299,664 members

Serialization for Rapid Application Development: A Better Approach

Rate this:
5.00 (6 votes)
Please Sign up or sign in to vote.
5.00 (6 votes)
25 Apr 2008CPOL
Serialize and Deserialize your objects with ease and simplicity
Image 1


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:

// example of registering class and all derived classes
// example of registering class with an unrelated class
// 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.


  • 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


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


About the Author

Daniel Gidman
Architect Stackify
United States United States
No Biography provided

Comments and Discussions

QuestionYour approach versus DataContractSerializer Pin
Denis Vuyka20-May-08 20:32
memberDenis Vuyka20-May-08 20:32 
AnswerRe: Your approach versus DataContractSerializer Pin
Daniel Gidman21-May-08 1:44
professionalDaniel Gidman21-May-08 1:44 
QuestionSample? Pin
Mr.PoorEnglish26-Apr-08 3:56
memberMr.PoorEnglish26-Apr-08 3:56 
GeneralRe: Sample? [modified] Pin
Daniel Gidman26-Apr-08 9:34
professionalDaniel Gidman26-Apr-08 9:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 25 Apr 2008


33 bookmarked