在DataContract Serializer中停止循环引用

时间:2012-05-16 18:01:12

标签: c# xml-serialization

使用数据合同序列化对象时,我遇到了循环引用的问题。我知道使用IsReference = true但我不能使用它,因为我继承了我无法控制的对象。有没有办法绕行,我可以计算时间并退出,就像休息一样吗?

2 个答案:

答案 0 :(得分:1)

按照此处的建议实现自定义DataContractSerializerBehavior:Circular References and WCF我应该修复它吗?

针对非WCF进行了更新:

链接答案中的解决方案基于使用:

new DataContractSerializer(type, name, ns, knownTypes,
                          0x7FFF //maxItemsInObjectGraph,
                          false //ignoreExtensionDataObject,
                          **true** //preserveObjectReferences,
                          null //dataContractSurrogate);

我认为使用这个构造函数应该修复它。

答案 1 :(得分:-2)