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

情到浓时终转凉″ 提交于 2019-12-21 12:45:20

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/739052/runtimetypehttp-schemas-datacontract-org-2004-07-system-is-not-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!