Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Serializing the Unserializable

0.00/5 (No votes)
7 Mar 2011 1  
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. 

C#
//Save the Strokes
var valuesaveDto = new StrokeDtoCollection(myInkPresenter.Strokes);
_myInkSaver.SaveMyData(valuesaveDto,"MyInk.ink");

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

image.png

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here