运行时类型:http://schemas.datacontract.org/2004/07/System' 不是预期的
好的,我让 DataContractSerializer 处理我的对象图。 请参阅我之前的问题以获取更多信息。
但是,我的一个字段 _UserPropertyDefinitions 的定义如下所示。它定义了该用户可以添加到数据结构中的对象的自定义属性列表。 字符串是标识属性的唯一键,Type 是属性的类型,始终是基本类型,如 Bool、Int、String 等。
每个对象都有一个对应的 Dictionary(String key, Object value) ) 集合来存储它为任何“用户属性”设置的值,
[DataMember]
private Dictionary<string, Type> _UserPropertyDefinitions;
当此属性是空集合时,我的对象图序列化得很好,但是一旦我向此集合添加自定义属性,我就会收到以下异常:尝试使用 DataContractSerializer 进行序列化。
输入带有数据的“System.RuntimeType” 合同名称 'RuntimeType:http://schemas.datacontract.org/2004/07/System ' 预计不会。 添加任何类型不 静态已知已知列表 类型 - 例如,通过使用 KnownTypeAttribute 属性或通过 将它们添加到已知类型列表中 传递给 DataContractSerializer。
如果我删除此字段的 DataMember 属性,我可以序列化/反序列化而不会出现异常,但当然我会丢失在此字段中创建的设置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定
Type
不会很好地序列化 - 并且可以说它无论如何都不属于数据契约,因为(特定于实现)它违背了主要目标之一数据契约...但是,我希望最好的方法是使用
Type
的将其替换为
或Dictionary
>AssemblyQualifiedNameFullName
。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 theType
'sAssemblyQualifiedName
orFullName
.