2010-07-19 34 views
3

我想用c#單聲道(單聲道v2.6)序列化一個複雜的圖形,圖形有雙向鏈接,並且有創建循環依賴關係的對象。單聲道是否支持帶有preserveObjectReferences標誌的DataContractSerializer?

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

我得到的異常如下:做一些閱讀中,我嘗試設置preserveObjectReferences標誌,它應該允許循環引用進行設置(此構造)後

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

有沒有人有任何運氣序列化單聲道的複雜對象?根據這裏的文檔: http://go-mono.com/status/status.aspx?reference=3.5&profile=2.0&assembly=System.Runtime.Serialization支持這些構造函數。

+0

我不能回答你的問題,但我會建議你試試你的代碼與最新的單聲道行李箱,看看它是否在那裏工作。 WCF自2.6以來已經修復了很多... – TheNextman 2010-07-19 13:27:41

回答

6

編輯:從我看到你還需要設置IsReference =在DataContractAttribute真實屬​​性,看​​和here

IsReference獲取或設置一個值,該值指示是否保留對象引用數據。 PreserveObjectReferences應該得到一個值,該值指定是否使用非標準XML結構來保存對象引用數據。

單不支持數據合同序列化,但由於此評論的人士告訴做,有做了一些工作:

([MonoTODO(「支持陣列;支持Serializable接口;支持SharedType;使用DataContractSurrogate」) ] - preserveObjectReferences表示輸出是否應該包含ms:Id。)

嘗試閱讀.NET Framework文檔中的Data Contract Serialization here,並將其與Mono中提供的源代碼進行比較,這將澄清事情。

也做同樣的其他System.Runtime.Serialization命名空間,從MSDN閱讀文檔,並與你Mono namespaces

在單對System.Runtime.Serialization命名空間的源代碼可有什麼比較here和DataContractSerializer的類源是here包含以下感興趣的東西:

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

//初始化()方法

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可製成通過構建DataContractSerializer的**當傳遞true爲參數PreserveObjectReference保護對象的引用:

new DataContractSerializer(type, name, ns, knownTypes, 
      0x7FFF /*maxObjectsInGraph*/, 
      false/*ignoreExtensionDataObject*/, 
      true/*preserveObjectReferences*/, 
      null/*dataContractSurrogate*/); 
相關問題