好的,我让DataContractSerializer使用我的对象图 . 有关更多信息,请参阅我之前的问题
Serialization / Derialization of a tree structure
The deserializer has no knowlege of any type that maps to this contract
但是,我的一个字段 _UserPropertyDefinitions 的定义如下所示 . 它定义了此用户可以添加到数据结构中的对象的自定义属性列表 . 字符串是标识属性的唯一键,Type是属性的类型,它始终是一个主要类型,如Bool,Int,String等 .
每个对象都有一个对应的 Dictionary(String key, Object value) 集合来存储它为"User Properties"中的任何一个设置的值 .
[DataMember]
private Dictionary<string, Type> _UserPropertyDefinitions;
当此属性为空集合时,我的对象图序列化很好,但是当我向此集合添加自定义属性时,在尝试使用DataContractSerializer进行序列化时会出现以下异常 .
输入数据合约名称为'RuntimeType:http://schemas.datacontract.org/2004/07/System'的'System.RuntimeType' . 将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中 .
如果我删除此字段的DataMember属性,我可以序列化/反序列化而不会获得异常,但当然我放弃了我在此字段中创建的设置 .
1 回答
我很确定
Type
不属于数据 Contract ,因为(具体实现)它违背了数据 Contract 的主要目标之一......但是,我希望最好的方法是使用
Type
的AssemblyQualifiedName
或FullName
交换Dictionary<string,string>
.