2010-11-24 43 views
6

看看here中的以下代碼。
這是關於在wcf中序列化時保留datacontract(對象模型,對象圖,域模型)中的循環引用。序列化代碼示例中的無盡循環

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方法?

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

現在也許這些方法沒有被使用?我在這裏錯過了什麼?

回答

3

確實是這樣。它的工作原理表明只有最後一個重載正在被調用。由於涉及不同的參數,因此可能失去靜態方法會更好(不起作用):

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