数据契约中的 IsReference 属性 [英] IsReference property in data contract
问题描述
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
允许序列化可以相互引用的对象树.因此,对于 Employees
的列表,每个列表都有一个 Manager
的属性(他也是 Employee
),一个对 Manager 的引用可以保留每个
而不是将 Employee
的 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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!