I have a DateRange
object that represents the notion of Infinity via Static reference as shown below. As you see, the end points that define Infinity are also static references in a different class, DatePoint.Past
and DatePoint.Future
.
Now I need to serialize this (as part of a deep Clone method that uses serialization) and know when it's deserialized that an instance with DateTime.Min
and DateTime.Max
as endpoints then the instance should be DateRange.Infinity
. So I think I need to make it ISerializable
.
My first attempt at implementing ISerializable
is quite poor; and but I'm showing it in the hopes it suggests a quicker fix to someone. I have used some similar code for NHibernate to store DateRange
in the db and reconstitue Infinity, but I'm not getting how to apply that for serialization yet.
DatePoint
is marked [Serializable]
but does not implement ISerializable
.
edited question
I am not looking to serialize/deserialize Infinity. What I am looking for is a hook into where I can take the deserialized DataRange
and decide if it is equivalent to Infinity
.
**
Cheers, Berryl
DateRange
[Serializable]
[TypeConverter(typeof(DateRangeTypeConverter))]
public class DateRange : ValueObject, IRange<DatePoint, DateRange, TimeSpan>, ISerializable
{
/// <summary>
/// Returns a range spanning <see cref="DatePoint.Past"/> and <see cref="DatePoint.Future"/>.
/// </summary>
public static readonly DateRange Infinity = new DateRange(DatePoint.Past, DatePoint.Future);
/// <summary> The start of the <see cref="DateRange"/> range. </summary>
public DatePoint Start { get; protected set; }
/// <summary> The end of the <see cref="DateRange"/> range. </summary>
public DatePoint End { get; protected set; }
}
DatePoint
public class DatePoint : ValueObject, IComparable<DatePoint>, IComparable<DateTime>, IComparable, IEquatable<DatePoint>, IEquatable<DateTime>, IFormattable
{
/// <summary>The undefined infinite past, smaller than any other date except itself.</summary>
public readonly static DatePoint Past = DateTime.MinValue;
/// <summary>The undefined infinite future, larger than any other date except itself.</summary>
public readonly static DatePoint Future = DateTime.MaxValue;
}
First ISerializable attempt
protected DateRange(SerializationInfo info, StreamingContext ctx) {
if (info == null)
throw new System.ArgumentNullException("info");
var start = (DatePoint)info.GetValue("Start", typeof(DatePoint));
var end = (DatePoint)info.GetValue("End", typeof(DatePoint));
// its Infinity if so
if((start.Equals(DatePoint.Past) && end.Equals(DatePoint.Future)))
return;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}