views:

1367

answers:

1

Ok so I got DataContractSerializer working with my object graph. See my previous questions for more information.

http://stackoverflow.com/questions/736568/serialization-derialization-of-a-tree-structure

http://stackoverflow.com/questions/736900/the-deserializer-has-no-knowlege-of-any-type-that-maps-to-this-contract

However, one of my fields, _UserPropertyDefinitions, is defined as shown below.. It defines a list of custom properties that this user can add to objects in the data structure. The string is a unique key to identify the property, and Type is the type of the property which is always a primative type like Bool, Int, String etc etc..

Each object has a corresponding Dictionary(String key, Object value) collection to store the values it has set for any of the "User Properties"

[DataMember]
private Dictionary<string, Type> _UserPropertyDefinitions;

My object graph serializes fine when this property is an empty collection, yet once I add a custom property to this collection I get the following exception when trying to serialize with DataContractSerializer.

Type 'System.RuntimeType' with data contract name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

If I remove the DataMember attribute for this field the I can serialize/deserialize with out getting an exception, but of course I loose the settings I've created in this field.

+2  A: 

I'm pretty sure that Type isn't going to serialize very well - and arguably it doesn't belong in a data-contract anyway, since (being implementation specific) it defeats one of the main aims of a data-contract...

However, I expect the best approach would be to swap that for a Dictionary<string,string>, using the Type's AssemblyQualifiedName or FullName.

Marc Gravell
Geeze Marc don't you have any real work to do? :-) But seriously I really appreciate the help. So basically I'd store the name of the Type, and then later retrieve the real Type from the name?I know how to find the Type's name, but how would I retrieve a Type later when I only know the name?
Eric Anastas