惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
腾讯CDC
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
量子位
F
Fortinet All Blogs
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
D
Docker
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
M
MIT News - Artificial intelligence
博客园 - 【当耐特】

博客园 - 无名365

.NET 2.0 JSON Parser Provisioning 是什么意思? SOA与云计算 Good understand on WCF Data Contract & Serialization a xml configuration for unity and interception Programmatically Compress and Decompress Files using c# CodeRun Studio - A free cloud develop and debug service for .Net Cloud IDE 10个最“优秀”的代码注释 Compress and Decompress JQuery Mobile 1.0 Released, Gets Mixed Reaction Interceptor in Unity and Policy Injection in Unity Configuring Unity Container with Policy Injection Configuration Unity Configuration Node Unity Configuration from Separate Configuration File IoC and Unity - Configuration Objects and the Art of Data Modeling AppFabric Cache - Cache Cluster EF - How to delete an object without retrieving it LINQ to SQL Extension: Batch Deletion with Lambda Expression
WCF DataContract EmitDefaultValue
无名365 · 2011-11-29 · via 博客园 - 无名365

2011-11-29 11:41  无名365  阅读(761)  评论()    收藏  举报

The WCF DataContract model is ‘opt-in’; to include a member in the contract, explicitly apply the DataMemberAttribute to the field or property to include it in the serialized output. What if I want some properties to ‘opt-out’ under certain scenarios, should I be able to do this at runtime?

One possible solution is the use of the Data Member Default Values (http://msdn.microsoft.com/en-us/library/aa347792.aspx) . In .NET, all types have the concept of a default value and uninitialised, these are the initial states of the types.

The System.Runtime.Serialization.DataMemberAttribute has a property EmitDefaultValue. By default it is true which is why you may see entries in the service response like

<sig i:nil="true" >

Set DataMemberAttribute = false, and when a property is in its ‘default’ state, it wont be included in the serialization.

For example a DataContract may be described by

[DataContract]

public class DataResponse

{

public DataResponse()

{

version = "0.6";

reqId = "0";

status = "ok";

}

[DataMember(EmitDefaultValue=false) ]

public String version { get; set; }

[DataMember(EmitDefaultValue = false)]

public String reqId { get; set; }

[DataMember(EmitDefaultValue = false)]

public String status { get; set; }

[DataMember(EmitDefaultValue = false)]

public String warnings { get; set; }

[DataMember(EmitDefaultValue = false)]

public String errors{get;set;}

[DataMember(EmitDefaultValue = false)]

public String sig { get; set; }

[DataMember(EmitDefaultValue = false)]

public String table { get; set; }

}

With some properties initialised on creation, others to be completed at runtime.

If I only want to serialize the errors property when the status is equal to “errors” then EmitDefaultValue = false excludes the errors property until it is explicity set.

if (this.item.status == "errors")

{

this.item.errors = "...some error information structure...";

}

Whereupon the errors property will be included in the serialization.

Likewise by setting the property back to the default value

this.item.errors = null;

excludes it again.

This is a fairly simplistic approach to controlling the serialized structure, however it is the least intrusive without getting into Reflection and the bowels of the serialization process.