Archive: Evil Bug – Serialization of TimeSpan object.

From: http://blogs.ssw.com.au/andrewweaver/archive/2004/05/22/201.aspx

I’m using XML Serialization to get the XML for one my classes for public storage. This class has a public property of type TimeSpan… My evil bug is that despite the doco saying that the timespan would get serialized, it didn’t!

Resolved it by making the main property be ignored by serializer, and then a new property based on the Ticks for the TimeSpan:

[XmlIgnore]
public TimeSpan DailyInterval
{
    get { return m_Interval; }
    set { m_Interval = value; }
}

public long DailyIntervalTicks
{
    get { return m_Interval.Ticks; }
    set { m_Interval = new TimeSpan(value); }
}

Some other dude had the same problem (along with many others) and attempted to explain it…

Advertisements

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