首页 文章

DataContractSerializer在对象列表中将重复的属性引用反序列化为null

提问于
浏览
2

一切都很好,对象正在使用datacontractserializer进行序列化和反序列化 . 然而,我的一位同事为我创建了一个样本,它没有按照预期的那样进行 . 场景是这样的:我们有一个对象列表,它们由作为对象引用的属性组成,因为这些是从edmx创建的POCO实体,所以我将已将PreserveObjectReferences设置为"true", but now if two items in the list contain the same property reference only one of them deserializes and the other one will have its property deserialized as null!!!! 的循环引用

DataContractSerializer配置如下:

编辑:解决我自己的问题后,我只发布序列化的简化版本 .

even in a simplified version I used the same serializer for serialization and deserialization, and same MemoryStream, it is still the same, deserialization does not yield the orignal list :((

returnType = result.GetType().ToString();
serializer = new DataContractSerializer(result.GetType(), null, 0x7fff, false, true, null);
tempStream = new MemoryStream();
serializer.WriteObject(tempStream, result);
tempStream.Position = 0;
serializedReturn = new StreamReader(tempStream).ReadToEnd();
tempStream.Position = 0;
object res = serializer.ReadObject(tempStream);

POCO实体由T4模板创建,名称为“POCO实体生成器”,可以在visual studio的扩展在线图库中找到 . 它们没有使用datacontract和datamember属性进行修饰,但它仍然有效 .

我只是不知道可能出现什么问题,因为如果我没有重复参考,一切都很好,有什么想法吗?提前致谢 .

1 回答

  • 1

    好的,既然没人回答或帮忙,我自己找到了解决办法 . 不幸的是,.NET自己的DataContractSerializer出现了错误,而是使用了James NewtonKing的Json.NET,他为了制作一个适用于我的情况的优秀序列化器,而微软却没有 .

    http://json.codeplex.com/

相关问题