在将参数传递给基类时,如何反序列化派生类

时间:2014-09-18 14:57:37

标签: c# .net serialization derived-class binary-serialization

我有一个派生类,它继承自绘制圆圈的贝司类

    public BraceHole(Brace brace, Vertex centerPoint, double diameter, VDrawI5.vdEntities entities) : base(centerPoint, diameter / 2, entities)
    {
        this.brace = brace;
        this.centerPoint = centerPoint;
        this.radius = diameter/2;
    }

我正在尝试使用二进制序列化来序列化此类。

我像这样序列化:

   public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("brace", brace, typeof(Brace));
        info.AddValue("radius", radius, typeof(double));
        info.AddValue("centerPoint", centerPoint, typeof(Vertex));
    }

序列化似乎没问题,但是当我像下面那样对它进行序列化时;我正在获取数据字段(大括号,半径和centerPoint);但是我没有把圈子拿回来!我怀疑是因为我没有对基类进行序列化。不过我试试,我不知道怎么做!

    protected BraceHole(SerializationInfo info, StreamingContext context)
    {
       try {
            brace = (Brace)info.GetValue("brace", typeof(Brace));
            radius = (double)info.GetValue("radius", typeof(double));
            centerPoint = (Vertex)info.GetValue("centerPoint", typeof(Vertex));
        }
       catch{} 
    }

我的问题是如何反序化。 我正在寻找这样的东西,这可能吗?

  protected BraceHole(SerializationInfo info, StreamingContext context) : base(centerPoint, radius, entities)
    {
       VdProControl.VdProControl vectorDraw = (VdProControl.VdProControl)context.Context;
       VDrawI5.vdEntities entities = vectorDraw.ActiveDocument.Entities;

       try {
            brace = (Brace)info.GetValue("brace", typeof(Brace));
            radius = (double)info.GetValue("radius", typeof(double));
            centerPoint = (Vertex)info.GetValue("centerPoint", typeof(Vertex));
        }
       catch{} 
    }

1 个答案:

答案 0 :(得分:1)

你需要做的是序列化基类(你当前没有做)并在基类中有一个受保护的构造函数,它接受SerializationInfoStreamingContext反序列化它。 / p>

//This is now a override of the virtual function defined in Circle
public override void GetObjectData(SerializationInfo info, StreamingContext context)     
{
    base.GetObjectData(info, context); //Get the base class data
    info.AddValue("brace", brace, typeof(Brace));
    info.AddValue("radius", radius, typeof(double));
    info.AddValue("centerPoint", centerPoint, typeof(Vertex));
}

protected BraceHole(SerializationInfo info, StreamingContext context) 
    : base(info, context) //Deserialize the base class data.
{
   // The commented code would likely now go in the base class's protected constructor.
   //VdProControl.VdProControl vectorDraw = (VdProControl.VdProControl)context.Context;
   //VDrawI5.vdEntities entities = vectorDraw.ActiveDocument.Entities;

   try {
        brace = (Brace)info.GetValue("brace", typeof(Brace));
        radius = (double)info.GetValue("radius", typeof(double));
        centerPoint = (Vertex)info.GetValue("centerPoint", typeof(Vertex));
    }
   catch
   {
       //This empty try-catch is a bad idea, if something goes wrong you should 
       //at minimum log it some how. It would be better to get rid of it and let 
       //the caller catch the exception.
   } 
}

您需要修改Circle以获得新的构造函数和虚拟方法GetObjectData