Click here to Skip to main content
13,794,050 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 25 May 2011
Licenced GPL3

Serializable Extra Types for .NET 4

, 25 May 2011
Rate this:
Please Sign up or sign in to vote.
Serialization for Rapid Application Development.

You may follow this code @


This is an updated article and code base from my original publication [Serialization for Rapid Application Development: A Better Approach] here at CodeProject. After several years of growth in ability and advances in technology, I decided it was time to update that code base and article for use in .NET 4. The result of this is a much smaller code base, unit tests to back them up, and fixes to some issues concerning discovery within a web environment.


In short, the problems with the XmlSerializer that existed when I wrote the first article still exist today. They are classic issues that there is still no unified approach in dealing with. The major problem is still how do you resolve unknown classes when serializing and deserializing.

There have been several attempts that I've followed over the years to get around this, anywhere from the creation of entire frameworks like YAXLib to instructions on how to make use of the IXmlSerializable interface at various levels of sophistication.

With all of these solutions, the major problem from a developer perspective that has always crept in is cumbersomeness of the implementation. IXmlSerializable asks the developer to write some form of a customized implementation per class. YAXLib and others ask the developer to use different attributes, or handle outputs that don't look like the clean(ish) XML generated by XmlSerializer. The [XmlInclude(Type type)] attribute demands that all types to be serialized be within the same library. None of them (that I know of) in my opinion relieves the developer from the tedium of working with these solutions.

Goals Realized

Serializable Extra Types is designed to be as thoughtless as possible with an absolute minimum of development consideration. What it does is make use of the standard XmlSerializer and a slightly un-hyped overloaded constructor; it has to incorporate extra type definitions for use in type resolution during serialization and deserialization.

It is actually a very simple idea. Keep a list of all the possible types that the XmlSerializer may have need of during a serialization/deserialization process. Register those types using attribute adornments and provide some Extension Methods to make incorporating those lists thoughtless to the developer.

It works under a parent child relationship. I can best describe it as saying, it's the reverse of the XmlInclude attribute. The XmlInclude attribute is placed on a class to give the serializer knowledge of other classes when serializing the class that is adorned. SerializableExtraType is placed on a class to give the other class knowledge of the adorned class when the other class is serialized.


XmlInclude = Parent => Child
SerializableExtraType = Child => Parent

This allows the SerializableExtraTypes code to integrate related classes across libraries. Additionally, I have exposed a method by which you may register additional relationships at runtime. This solves any situation that you may come across with libraries and applications having complex implied relationships.

Using the code

The code is available in the download and at the CodePlex site. Both have a series of test libraries and a consuming test project that shows the usage very well. Here I will outline the quick and dirty of how to make use of it.

First, adorn a class with the required attribute like this:

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

The Extension Methods make use of the SerializableExtraTypes under System.Xml.Serialization.

Now make use of the Extension Methods to serialize and deserialize objects.

// example 1 assuming that ClassOne and ClassTwo inherit from Foo
string xml = new Foo { ClassList = {new ClassOne(), new ClassTwo(), }, }.SerializeEx();
Foo obj = "<Foo><ClassList><ClassOne /><ClassTwo /></Foo>".DeserializeEx<Foo>();

// example 2 assuming that Foo bears no relationship with SomethingElse
string xml = new SomethingElse { ObjectList = { new Foo(), new Foo(), }, }.SerializeEx();
SomethingElse obj = "<SomethingElse><ArrayOfObject><Foo />" + 
   "<Foo /></ArrayOfObject></SomethingElse>".DeserializeEx<SomethingElse>();

There is more functionality built into the code base but this is a quick and dirty sample. Please take a look at the download for further examples.


This is the second 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 GNU General Public License (GPLv3)


About the Author

Daniel Gidman
Architect Stackify
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionProposal cleanup Pin
Kabwla.Phone21-Sep-11 4:10
memberKabwla.Phone21-Sep-11 4:10 
AnswerRe: Proposal cleanup Pin
Daniel Gidman21-Sep-11 4:44
professionalDaniel Gidman21-Sep-11 4:44 
GeneralRe: Proposal cleanup Pin
Kabwla.Phone21-Sep-11 4:48
memberKabwla.Phone21-Sep-11 4:48 
GeneralRe: Proposal cleanup Pin
Daniel Gidman21-Sep-11 5:20
professionalDaniel Gidman21-Sep-11 5:20 
GeneralRe: Proposal cleanup Pin
Kabwla.Phone21-Sep-11 5:36
memberKabwla.Phone21-Sep-11 5:36 
QuestionThis looks very promising! Pin
Bob Sandberg1-Aug-11 8:36
memberBob Sandberg1-Aug-11 8:36 
AnswerRe: This looks very promising! Pin
Daniel Gidman1-Aug-11 9:10
professionalDaniel Gidman1-Aug-11 9:10 
GeneralMy vote of +5 Pin
BillWoodruff14-Jun-11 3:59
mentorBillWoodruff14-Jun-11 3:59 
GeneralRe: My vote of +5 Pin
Daniel Gidman14-Jun-11 5:45
professionalDaniel Gidman14-Jun-11 5:45 
GeneralMy vote of 5 Pin
Filip D'haene30-May-11 3:31
memberFilip D'haene30-May-11 3:31 
GeneralRe: My vote of 5 Pin
Daniel Gidman30-May-11 14:50
professionalDaniel Gidman30-May-11 14:50 
GeneralI could be missing something here, but Pin
Sacha Barber25-May-11 3:42
mvpSacha Barber25-May-11 3:42 
GeneralRe: I could be missing something here, but Pin
Daniel Gidman25-May-11 4:02
professionalDaniel Gidman25-May-11 4:02 
GeneralRe: I could be missing something here, but Pin
Sacha Barber25-May-11 4:30
mvpSacha Barber25-May-11 4:30 
GeneralRe: I could be missing something here, but Pin
Daniel Gidman25-May-11 4:44
professionalDaniel Gidman25-May-11 4:44 
GeneralRe: I could be missing something here, but Pin
Sacha Barber25-May-11 5:06
mvpSacha Barber25-May-11 5:06 
QuestionRe: I could be missing something here, but Pin
John B Oliver15-Jun-11 13:07
memberJohn B Oliver15-Jun-11 13:07 
Daniel, so to paraphrase your entire article, this provides a mechanism to serialise a child class by simply registering the base class? And this mechanism works even if you do not have access to the source code of the base class?
AnswerRe: I could be missing something here, but [modified] Pin
Daniel Gidman15-Jun-11 13:28
professionalDaniel Gidman15-Jun-11 13:28 
GeneralRe: I could be missing something here, but Pin
Jefis25-May-11 5:10
memberJefis25-May-11 5:10 
GeneralRe: But will it throw? Pin
Kabwla.Phone21-Sep-11 4:22
memberKabwla.Phone21-Sep-11 4:22 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.181207.3 | Last Updated 25 May 2011
Article Copyright 2011 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid