Does mono DataContractSerializer support the preserveObjectReferences flag?

I am trying to serialize a complex graph using C # mono (mono v2.6), the graph has bidirectional links and there are objects that create circular dependencies. After doing some reading, I tried to set the preserveObjectReferences flag, which should allow the setting of circular references (this constructor):

public DataContractSerializer(
 Type type,
 IEnumerable<Type> knownTypes,
 int maxItemsInObjectGraph,
 bool ignoreExtensionDataObject,
 bool preserveObjectReferences,
 IDataContractSurrogate dataContractSurrogate
)

An exception I get the following:

SerializationException: Circular reference of an object in the object graph was found: 'ShaderMasterNode' of type ShaderMasterNode

Has anyone managed to serialize complex objects in mono? According to the documentation here: http://go-mono.com/status/status.aspx?reference=3.5&profile=2.0&assembly=System.Runtime.Serialization These constructors are supported.

+3
source share
1

EDIT: , , IsReference = true DataContractAttribute, .

IsReference , , . PreserveObjectReferences , , XML- .

Mono , , , :

([MonoTODO ( " , Serializable, SharedType, DataContractSurrogate" )] - preserveObjectReferences , ms: Id .)

.NET Framework , Mono, .

System.Runtime.Serialization, MSDN ,

System.Runtime.Serialization Mono , DataContractSerializer - , :

// Three constructors with this property

    public DataContractSerializer (Type type,
   IEnumerable<Type> knownTypes,
   int maxObjectsInGraph,
   bool ignoreExtensionDataObject,
   **bool preserveObjectReferences,**
   IDataContractSurrogate dataContractSurrogate)
   : this (type, knownTypes)
  {
   Initialize (maxObjectsInGraph,
    ignoreExtensionDataObject,
    **preserveObjectReferences,**
    dataContractSurrogate);
  }

  public DataContractSerializer (Type type,
   string rootName,
   string rootNamespace,
   IEnumerable<Type> knownTypes,
   int maxObjectsInGraph,
   bool ignoreExtensionDataObject,
   **bool preserveObjectReferences,**
   IDataContractSurrogate dataContractSurrogate)
   : this (type, rootName, rootNamespace, knownTypes)
  {
   Initialize (maxObjectsInGraph,
    ignoreExtensionDataObject,
    **preserveObjectReferences,**
    dataContractSurrogate);
  }

  public DataContractSerializer (Type type,
   XmlDictionaryString rootName,
   XmlDictionaryString rootNamespace,
   IEnumerable<Type> knownTypes,
   int maxObjectsInGraph,
   bool ignoreExtensionDataObject,
   **bool preserveObjectReferences,**
   IDataContractSurrogate dataContractSurrogate)
   : this (type, rootName, rootNamespace, knownTypes)
  {
   Initialize (maxObjectsInGraph,
    ignoreExtensionDataObject,
    **preserveObjectReferences,**
    dataContractSurrogate);
  }

// Initialize()

    void Initialize (
   int maxObjectsInGraph,
   bool ignoreExtensionDataObject,
   bool preserveObjectReferences,
   IDataContractSurrogate dataContractSurrogate)
  {
   if (maxObjectsInGraph < 0)
    throw new ArgumentOutOfRangeException ("maxObjectsInGraph must not be negative.");
   max_items = maxObjectsInGraph;
   ignore_ext = ignoreExtensionDataObject;
   preserve_refs = preserveObjectReferences;
   surrogate = dataContractSurrogate;

   PopulateTypes (Type.EmptyTypes);
  }

// preserveObjectReferences()

    public bool PreserveObjectReferences {
   get { return preserve_refs; }
  }

this:

** DataContractSerializer; , , . () (, ), .

DataContractSerializer true PreserveObjectReference DataContractSerializer **:

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

Source: https://habr.com/ru/post/1755309/


All Articles