Monday, July 28, 2008

ValueSerializer and TypeConverter

Ran into an interesting situation today. I have a complex class that's stored in a database. I want to serialize a reference to this class to disk. So I've implemented a ValueSerializer that stores the primary key of the database object in Xaml, and I've associated this ValueSerializer with the class via the ValueSerializerAttribute.

It serializes fine. But the XamlReader throws an exception upon deserialization:

"The Element type 'blah' does not have an associated TypeConverter to parse the string 'blah blah blah'.

Now, I'd like to point out that I've provided overriden implementations for CanConvertFromString and ConvertFromString in my ValueSerializer. One would think that this should take care of the problem, as I didn't need a TypeConverter to serialize in the first place.

But alas, I have to implement a TypeConverter between my class and String, which simply invokes the ValueSerializer to perform a conversion. Now it works, but it seems like I'm using two classes where I should only have one.

I'm surprised that ValueSerializer doesn't inherit from TypeConverter, as it seems to be a specialized case. Or did I just forget to implement something necessary for ValueSerializer to pick up on this conversion? There's surprisingly little documentation about this class, which seems to be so critical to anyone doing custom Xaml development.

Saturday, July 26, 2008

This Blog

This blog got started thanks to the encouragement of two of my coworkers:

I'd been keeping an internal blog at my company, posting issues I'd had to solve on my own because the necessary information wasn't readily available or it took me a long time to find it. They've encouraged me to make these posts available to the .Net community at large, so here we go!