Click here to Skip to main content
Licence CPOL
First Posted 25 Apr 2008
Views 17,168
Downloads 135
Bookmarked 31 times

Serialization for Rapid Application Development: A Better Approach

By | 25 Apr 2008 | Article
Serialize and Deserialize your objects with ease and simplicity

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

Architect
Ascend Learning
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionYour approach versus DataContractSerializer PinmemberDenis Vuyka20:32 20 May '08  
AnswerRe: Your approach versus DataContractSerializer PinmemberDaniel Gidman1:44 21 May '08  
QuestionSample? PinmemberMr.PoorEnglish3:56 26 Apr '08  
Sorry, I'm not very familiar with Serialization (what the ... is "root"?), here are my trials:
using System;
using System.Collections.Generic;
using System.Text;
 
namespace SerializableExtraTypes {
   static class Program {
      static void Main() {
         List<foo> Lst = new List<Foo>();
         Lst.Add(new Foo(1, 2));
         Lst.Add(new Foo(3, 4));
         //string S = SerializableExtraTypes.Serialize(Lst, typeof(Foo));
         string S = SerializableExtraTypes.Serialize(Lst, typeof(List<foo>));
         //string S = SerializableExtraTypes.Serialize(Lst, null);  
      }
   }
 
   [Serializable]
   // example of registering class and all derived classes
   [SerializableExtraType(typeof(Foo))]
   public class Foo {
      private int myVar;
      private int myVar2;
 
      public Foo() { }
      public Foo(int Var, int Var2) { myVar = Var; myVar2 = Var2; }
 
      public int MyProperty {
         get { return myVar; }
         set { myVar = value; }
      }
 
      public int MyProperty2 {
         get { return myVar2; }
         set { myVar2 = value; }
      }
   }
}</foo></foo>
what's wrong with it?
Ah, innerexception: "the type SerializableExtraTypes.Foo in the Assembly SerializableExtraTypes, couldn't be loaded"
at: SerializableExtraTypes.cs:Zeile 36 , which is this one:
 
s.Serialize(xw, obj);
 
in: SerializableExtraTypes.Serialize(object obj, format, root)
GeneralRe: Sample? [modified] PinmemberDaniel Gidman9:34 26 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid