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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
Y
Y Combinator Blog
IT之家
IT之家
博客园 - 聂微东
L
LangChain Blog
爱范儿
爱范儿
H
Help Net Security
GbyAI
GbyAI
F
Fortinet All Blogs
B
Blog
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
D
DataBreaches.Net
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享

博客园 - 无名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.