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

推荐订阅源

T
Threatpost
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Recent Commits to openclaw:main
Recent Commits to openclaw:main
WordPress大学
WordPress大学
Webroot Blog
Webroot Blog
Jina AI
Jina AI
H
Hacker News: Front Page
Attack and Defense Labs
Attack and Defense Labs
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
P
Privacy International News Feed
A
Arctic Wolf
IT之家
IT之家
云风的 BLOG
云风的 BLOG
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
博客园 - 司徒正美
Vercel News
Vercel News
C
Cyber Attacks, Cyber Crime and Cyber Security
SecWiki News
SecWiki News
K
Kaspersky official blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
S
Schneier on Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队
PCI Perspectives
PCI Perspectives
F
Fortinet All Blogs
T
Tenable Blog
Spread Privacy
Spread Privacy
T
The Blog of Author Tim Ferriss
S
Securelist
L
LangChain Blog
Latest news
Latest news
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)

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