RuntimeType:http://schemas.datacontract.org/2004/07/System' is not expected
Asked Answered
F

1

7

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

Serialization / Derialization of a tree structure

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.

Forcier answered 10/4, 2009 at 22:22 Comment(1)
Sorry for delayed response ;-p Either Type.GetType() or Assembly.GetType()Tumular
T
7

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.

Tumular answered 10/4, 2009 at 22:25 Comment(4)
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?Forcier
@Marc Gravell how would you retrieve the type when you only have the name if it isn't a known farmework type , would you also send the entire assembly some how ?Trihedral
@eran well, an AssemblyQualifiedName isn't "just" a name, but yes it is problematic, especially if the assemblies are different. If the two implementations can categorise their known types,maybe an enum would suffice? In protobuf-net, for serialising Type I let the user provide an implementation for Type <==> string, so they can map it manually.Tumular
@MarcGravell i need to pass a custom Type from my client to a wcf service , this can't be done since the service does not know the Type i'm sending . any work arounds come to mind for this scenario ?Trihedral

© 2022 - 2024 — McMap. All rights reserved.