参与反序列化过程 [英] hooking into the deserialization process

查看:147
本文介绍了参与反序列化过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DateRange对象,它通过静态引用"表示无限"的概念,如下所示.如您所见,定义Infinity的端点也是DatePoint.PastDatePoint.Future类别中的静态引用.

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.

现在,我需要对此序列化(作为使用序列化的深层克隆方法的一部分),并且知道在反序列化以DateTime.MinDateTime.Max作为端点的实例时,该实例应该为DateRange.Infinity .所以我想我要把它做成ISerializable.

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.

我第一次实现ISerializable的尝试很差.但我正在展示它,希望它可以为某个人提供更快的解决方案.我已经为NHibernate使用了一些类似的代码将DateRange存储在数据库中并重构了Infinity,但是我还没有获得如何将其应用于序列化的方法.

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被标记为[Serializable],但未实现ISerializable.

DatePoint is marked [Serializable] but does not implement ISerializable.

我不打算对Infinity进行序列化/反序列化.我 am 寻找的是一个钩子,可以在其中获取反序列化的DataRange并确定其是否等同于Infinity.

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.

干杯, 贝里

[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;

}

第一次可序列化的尝试

    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)
    {
    }

推荐答案

您可以实现IObjectReference并在反序列化之后替换对象:

You can implement IObjectReference and replace the object after deserialization:

object IObjectReference.GetRealObject(StreamingContext context)
{
    if (Start.Equals(DatePoint.Past) && End.Equals(DatePoint.Future))
    {
        return Infinity;
    }
}

请参见文档.

这篇关于参与反序列化过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆