Click here to Skip to main content
13,000,469 members (61,981 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 7 Mar 2011

Serializing the Unserializable

, 7 Mar 2011
Rate this:
Please Sign up or sign in to vote.
How to serialize a class that is not serializable.

I came across a post in the Windows Phone developer forums that got me thinking for a moment. The poster had asked how to serialize a class that was not serializable. The class of interest was the StrokeCollection class. The solution for this problem has actually been around for a while but under different terms. If you pick up a Design Patterns book and look up Data Transfer Object (DTO), you'll see what I'm talking about. For the sake of those that don't have a Design Patterns book handy, I'll describe the concept.

A DTO exists for the sake of storing data in an object that is suitable for crossing boundaries. More times than not, when I see some one talking about a DTO, it is for an object that is going from an application to a database. But it's not restricted to that scenario. For the sake of answering the question for the poster, I can assume that he or she wanted to transfer the data to a file. The representation of the data in a file is also a form that is suitable for transfer over a wire.

To accomplish the task, I made a few classes that have some (but not all) of the same properties as the classes I needed to serialize. I didn't need to replicate all of the properties because I'm not interested in saving all of the data that the class provides. As far as functionality goes, the only thing that I've put on these classes is functions for converting from the DTO to the original class and vice versa. The StrokeCollection class uses a number of other classes to represent its data. StrokeCollection contains instances of Stroke, which contains instances of StylusPointCollection, and that contains instances of StylusPoint. I created a similar hierarchy with the members of that hierarchy marked as serializable (with the [DataContract] attribute). For saving and loading the data, I've made use of the serialization code from a previous post. With my class hierarchy and the serialization code, I was able to load and save the strokes in a few lines of code. 

//Save the Strokes
var valuesaveDto = new StrokeDtoCollection(myInkPresenter.Strokes);

//Load the Strokes
var valueDto = _myInkSaver.LoadMyData("");
myInkPresenter.Strokes = valueDto.ToStrokeCollection();


I've put together an example program (see link at the top of article) that uses the code. Word of warning, I've not implemented any tomb-stoning code in it. But draw whatever you like and then click on the Save button. After you exit from the program, when you go back into it, you'll see your picture reload.


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


About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.

For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.

While most of my articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.


You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
seaDoc15-Mar-11 3:31
memberseaDoc15-Mar-11 3:31 
GeneralRe: My vote of 1 Pin
Joel Ivory Johnson30-Mar-11 19:50
memberJoel Ivory Johnson30-Mar-11 19:50 

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 | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 8 Mar 2011
Article Copyright 2011 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid