问题描述
DataContract
中IsReference
属性的目的是什么?请求和响应如何随应用此属性而变化?
What is the purpose of IsReference
property in DataContract
? How does the request and response vary with this property applied?
推荐答案
默认情况下,它确定对象如何序列化IsReference=false
.
It determines how objects are serialized, by default, IsReference=false
.
设置IsReference = true
允许序列化可以相互引用的对象树.因此,对于每个都有一个Manager
属性(也是一个Employee
)的Employees
列表,可以保留对每个Employee
的Manager
引用,而不是将Manager
嵌入到其中每个Employee
节点:
Setting IsReference = true
allows the serialization of trees of objects that can reference each other. So with a list of Employees
that each have a property for Manager
(who is also an Employee
), a reference to the Manager
for each Employee
can be held rather than embedding the Manager
within each Employee
node:
IsReference=false
会产生:
<Employee>
<Manager i:nil="true" />
<Name>Kenny</Name>
</Employee>
<Employee>
<Manager>
<Manager i:nil="true" />
<Name>Kenny</Name>
</Manager>
<Name>Bob</Name>
</Employee>
<Employee>
<Manager>
<Manager i:nil="true" />
<Name>Kenny</Name>
</Manager>
<Name>Alice</Name>
</Employee>
IsReference=true
会产生:
<Employee z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<Manager i:nil="true" />
<Name>Kenny</Name>
</Employee>
<Employee z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<Manager z:Ref="i1" />
<Name>Bob</Name>
</Employee>
<Employee z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<Manager z:Ref="i1" />
<Name>Alice</Name>
</Employee>
摘录自此博客,其中有完整的说明,以及生成的带有示例的XML的示例.
Snippets taken from this weblog that has a full explanation along with examples of the generated XML with the property applied.
MSDN- IsReference属性提供详细信息以及可互操作对象引用.
MSDN - IsReference Property provides details as well as Interoperable Object References.
这篇关于数据协定中的IsReference属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!