序列化代码示例中的无限循环

发布于 2024-10-04 08:01:59 字数 1878 浏览 3 评论 0原文

查看以下代码 此处
它是关于在 wcf 中序列化时保留数据契约(对象模型、对象图、域模型)中的循环引用。

class ReferencePreservingDataContractSerializerOperationBehavior
      :DataContractSerializerOperationBehavior
    {
        public ReferencePreservingDataContractSerializerOperationBehavior(
          OperationDescription operationDescription)
          : base(operationDescription) { }

        public override XmlObjectSerializer CreateSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }
 
        private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }
 
        public override XmlObjectSerializer CreateSerializer(
          Type type, XmlDictionaryString name, XmlDictionaryString ns,
          IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes,
                0x7FFF /*maxItemsInObjectGraph*/,
                false/*ignoreExtensionDataObject*/,
                true/*preserveObjectReferences*/,
                null/*dataContractSurrogate*/);
        }
    }

CreateDataContractSerializer 不是会生成无限循环 (stackoverflow) - 因此前面的 CreateSerializer 方法也会生成无限循环吗?

private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

现在也许这些方法都没有被使用?我在这里缺少什么?

Have a look at the following code from here.
It's about preserving circular references in a datacontract (object model, object graph, domain model) when serializing in wcf.

class ReferencePreservingDataContractSerializerOperationBehavior
      :DataContractSerializerOperationBehavior
    {
        public ReferencePreservingDataContractSerializerOperationBehavior(
          OperationDescription operationDescription)
          : base(operationDescription) { }

        public override XmlObjectSerializer CreateSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }
 
        private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }
 
        public override XmlObjectSerializer CreateSerializer(
          Type type, XmlDictionaryString name, XmlDictionaryString ns,
          IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes,
                0x7FFF /*maxItemsInObjectGraph*/,
                false/*ignoreExtensionDataObject*/,
                true/*preserveObjectReferences*/,
                null/*dataContractSurrogate*/);
        }
    }

Isn't CreateDataContractSerializer generating an endless loop (stackoverflow) - and therefore also the preceding CreateSerializer method?

private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

Now maybe these methods are not in use? What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-10-11 08:01:59

看起来确实如此。它有效的事实表明当前仅调用了最后一个重载。由于涉及不同的参数,也许最好放弃静态方法(这没有帮助):

public override XmlObjectSerializer CreateSerializer(
  Type type, string name, string ns, IList<Type> knownTypes)
{
    return new DataContractSerializer(type, name, ns, knownTypes,
        0x7FFF /*maxItemsInObjectGraph*/,
        false/*ignoreExtensionDataObject*/,
        true/*preserveObjectReferences*/,
        null/*dataContractSurrogate*/);
}

public override XmlObjectSerializer CreateSerializer(
  Type type, XmlDictionaryString name, XmlDictionaryString ns,
  IList<Type> knownTypes)
{
    return new DataContractSerializer(type, name, ns, knownTypes,
        0x7FFF /*maxItemsInObjectGraph*/,
        false/*ignoreExtensionDataObject*/,
        true/*preserveObjectReferences*/,
        null/*dataContractSurrogate*/);
}

It indeed appears so. The fact that it works suggests that only the last overload is currently being invoked. Since there are different parameters involved, perhaps it would be better to lose the static method (that isn't helping):

public override XmlObjectSerializer CreateSerializer(
  Type type, string name, string ns, IList<Type> knownTypes)
{
    return new DataContractSerializer(type, name, ns, knownTypes,
        0x7FFF /*maxItemsInObjectGraph*/,
        false/*ignoreExtensionDataObject*/,
        true/*preserveObjectReferences*/,
        null/*dataContractSurrogate*/);
}

public override XmlObjectSerializer CreateSerializer(
  Type type, XmlDictionaryString name, XmlDictionaryString ns,
  IList<Type> knownTypes)
{
    return new DataContractSerializer(type, name, ns, knownTypes,
        0x7FFF /*maxItemsInObjectGraph*/,
        false/*ignoreExtensionDataObject*/,
        true/*preserveObjectReferences*/,
        null/*dataContractSurrogate*/);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文