本文介绍了数据协定中的IsReference属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataContractIsReference属性的目的是什么?请求和响应如何随应用此属性而变化?

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列表,可以保留对每个EmployeeManager引用,而不是将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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 16:37