My question is a continuation of How to serialize a TimeSpan to XML
I have many DTO objects which pass TimeSpan
instances around. Using the hack described in the original post works, but it requires me to repeat the same bulk of code in each and every DTO for each and every TimeSpan
property.
So, I came with the following wrapper class, which is XML serializable just fine:
#if !SILVERLIGHT
[Serializable]
#endif
[DataContract]
public class TimeSpanWrapper
{
[DataMember(Order = 1)]
[XmlIgnore]
public TimeSpan Value { get; set; }
public static implicit operator TimeSpan?(TimeSpanWrapper o)
{
return o == null ? default(TimeSpan?) : o.Value;
}
public static implicit operator TimeSpanWrapper(TimeSpan? o)
{
return o == null ? null : new TimeSpanWrapper { Value = o.Value };
}
public static implicit operator TimeSpan(TimeSpanWrapper o)
{
return o == null ? default(TimeSpan) : o.Value;
}
public static implicit operator TimeSpanWrapper(TimeSpan o)
{
return o == default(TimeSpan) ? null : new TimeSpanWrapper { Value = o };
}
[JsonIgnore]
[XmlElement("Value")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public long ValueMilliSeconds
{
get { return Value.Ticks / 10000; }
set { Value = new TimeSpan(value * 10000); }
}
}
The problem is that the XML it produces looks like so:
<Duration>
<Value>20000</Value>
</Duration>
instead of the natural
<Duration>20000</Duration>
My question is can I both "eat the cake and have it whole"? Meaning, enjoy the described hack without cluttering all the DTOs with the same repetitive code and yet have a natural looking XML?
Thanks.