.NET Specified Property and Serialization

In my day to day activities as a software consultant I often see developers make the same old mistakes.  A classic that came up again recently was the behaviour of the .NET serializer in relation to value types.  The developer generated a class from a schema I had provided, programatically set the properties on the generated class, serialized the class, then started pulling his hair out wondering why the integers were missing and his strings were there. 
 
When a class is generated either using XSD.exe or WSDL.exe default behavior is to take all optional value types including DateTime, boolean, integer, etc and add a specified property.  When setting an integer on the class the corresponding specified property must be set to true telling the serializer to serialize the property.
 
So why do reference types such as strings not have a specified property.  Basically reference types may have a null value that indicates to the serializer that there is no value to serialize.  Think of the specified properties generated for the serializer as a way of indicating null to the serializer for value types.  If the specified property is false then this indicates to treat the value type as being null. 
Advertisements
This entry was posted in .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s