Str*_*mit 2 c# mono serialization
我试图使用c#mono序列化复杂图形(mono v2.6)图形具有双向链接,并且存在创建循环依赖关系的对象.在做了一些阅读之后,我尝试设置了preserveObjectReferences标志,该标志应允许设置循环引用(此构造函数):
public DataContractSerializer(
Type type,
IEnumerable<Type> knownTypes,
int maxItemsInObjectGraph,
bool ignoreExtensionDataObject,
bool preserveObjectReferences,
IDataContractSurrogate dataContractSurrogate
)
Run Code Online (Sandbox Code Playgroud)
我得到的例外情况如下:
SerializationException: Circular reference of an object in the object graph was found: 'ShaderMasterNode' of type ShaderMasterNode
Run Code Online (Sandbox Code Playgroud)
有没有人有运气序列化单声道的复杂对象?根据此处的文档:http: //go-mono.com/status/status.aspx?reference = 3.5&profile = 2.0 &assembly = System.Runtime.Serialization支持这些构造函数.
编辑:从我看到你还需要在DataContractAttribute中设置IsReference = true属性,请参阅此处和此处
IsReference获取或设置一个值,该值指示是否保留对象引用数据. PreserveObjectReferences应该获取一个值,该值指定是否使用非标准XML构造来保留对象引用数据.
Mono确实支持数据合同序列化,但正如源代码中的评论所述,还有一些工作要做:
([MonoTODO("支持数组;支持Serializable;支持SharedType;使用DataContractSurrogate")] - preserveObjectReferences指示输出是否应包含ms:Id.)
尝试阅读这里的数据合同序列化的.NET Framework文档,并将其与Mono中提供的源代码进行比较,这将澄清一些事情.
对其他System.Runtime.Serialization命名空间也一样,从MSDN中读取文档并与Mono命名空间中的内容进行比较
Mono中的System.Runtime.Serialization命名空间的源代码在这里可用,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);
}
Run Code Online (Sandbox Code Playgroud)
// 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);
}
Run Code Online (Sandbox Code Playgroud)
// preserveObjectReferences()属性
public bool PreserveObjectReferences {
get { return preserve_refs; }
}
Run Code Online (Sandbox Code Playgroud)
根据这个:
**默认情况下,DataContractSerializer不保留对象引用; 多次引用的对象的值被多次序列化.如果对象是相互(循环)引用的一部分(例如,循环链接列表),则在序列化期间抛出异常.
在构造DataContractSerializer**时,可以通过为参数PreserveObjectReference传递true来使DataContractSerializer保留对象引用:
new DataContractSerializer(type, name, ns, knownTypes,
0x7FFF /*maxObjectsInGraph*/,
false/*ignoreExtensionDataObject*/,
true/*preserveObjectReferences*/,
null/*dataContractSurrogate*/);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2267 次 |
| 最近记录: |