在序列化代码示例中无限循环 [英] Endless loop in a code sample on serialization

查看:126
本文介绍了在序列化代码示例中无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看一看从这里 下面的代码。< BR>
这是关于在WCF序列化时,在datacontract保持循环引用(对象模型,对象图,领域模型)。

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*/);
        }
    }



不是 CreateDataContractSerializer 产生一个死循环(计算器) - 因此也在前 CreateSerializer

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?

推荐答案

这的确是显得如此。它工作的事实表明,只有在最后的过载,目前正在调用。由于有涉及不同的参数,也许它会更好地失去了静态方法(也就是没有帮助):

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*/);
}

这篇关于在序列化代码示例中无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆