Quick Tip – Using the ShouldSerializeXXX methods

September 2, 2009

no comments

Quick Tip – Using the ShouldSerializeXXX methods

Something that I Quick Tip – Using the ShouldSerializeXXX methods
encountered last week.
The ShouldSerialize methods
are optional methods that
you can provide for
a class property.
These methods are built as ShouldSerializePropertyName
and inside of them you can provide a check that will determine whether the
property should be serialized or not. Of course this can be achieved only in
serializable classes.
An example of use:

public string Text { get; set; }
 
public bool ShouldSerializeText()
{
    return string.IsNullOrEmpty(Text);
}

Very helpful if I don’t what to serialize data that is not available.

For further reading – ShouldSerialize and Reset Methods.

Enjoy!

DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>