错误:“反序列化器不知道映射到此合约的任何类型”?
我有一个标记为 [Serializable]
的类 Foo 并实现 ISerialized
。我正在尝试通过 DataContractSerializer 对其进行序列化。在 GetObjectData 中我这样做:
info.AddValue("Test", new[] { 1,2,3});
它失败了:
元素“:Test”包含“http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfint”数据协定的数据。反序列化器不知道映射到此合约的任何类型。将与“ArrayOfint”对应的类型添加到已知类型列表 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给 DataContractSerializer 的已知类型列表。
我尝试将 knownTypes
参数传递给 DataContractSerializer 构造函数 - 没有帮助。
I have a class Foo marked [Serializable]
and implementing ISerializable
. I'm trying to serialize it via DataContractSerializer. In GetObjectData I do this:
info.AddValue("Test", new[] { 1,2,3});
It fails with:
Element ':Test' contains data of the 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfint' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'ArrayOfint' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
I tried passing a knownTypes
arg to the DataContractSerializer constructor - didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
knownTypes
参数传递给 DataContractSerializer 构造函数不会有帮助。相反,将[KnownType(typeof(int[]))]
添加到 Foo 类本身。Passing a
knownTypes
arg to the DataContractSerializer constructor will not help. Instead, add[KnownType(typeof(int[]))]
to class Foo itself.