Windows窗体中的序列化/反序列化有多个级别

时间:2012-05-11 23:18:08

标签: c# visual-studio-2010 serialization deserialization windows-forms-designer

我目前正在使用Windows Form应用程序上的序列化/反序列化,具有多个级别的类。我花了5天时间尝试调试它(从带有断点的简单函数StepInto到按属性关闭属性),但是在Form类中发生了相同的TargetInvocationException。

我会先解释一下这个等级。

首先,最上面的一个是这样的:

[Serializable]
public enum SAI
{
    level1,
    level2,
    level3
}

[Serializable]
public class First:ISerializable
{
    public SAI something { get; set; }
    public double something1 { get; set; }
    public int something2 { get; set; }

我已经实现了自定义序列化/反序列化部分

public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("something", something,typeof(SAI));
        info.AddValue("something1", something2, typeof(double));
        info.AddValue("something2", something2,typeof(int));
    }

    private First(SerializationInfo info, StreamingContext context)
    {
        something = (SAI)info.GetValue("something", typeof(SAI));
        something1 = (double)info.GetValue("something1",typeof(double));
        something2 = (int)info.GetValue("something2",typeof(int));
    }

虽然下一堂课是这样的:

[Serializable]
public class Second:ISerializable
{
  public String something3 { get; set; }
  public int something4 { get; set; }
  public bool something5 { get; set; }
  public double something6 { get; set; }
  public System.Drawing.Bitmap something7 { get; set; }
}

和自定义序列化/反序列化部分:

private Second(SerializationInfo info, StreamingContext context)
    {
        something3 = (String)info.GetValue("something3",typeof(String));
        something4 = (int)info.GetValue("something4",typeof(int));
        something5 = (bool)info.GetValue("something5",typeof(bool));
        something6 = (double)info.GetValue("something6",typeof(double));
        something7 = (System.Drawing.Bitmap)info.GetValue("something7", typeof(System.Drawing.Bitmap));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("something3", something3,typeof(String));
        info.AddValue("something4", something4,typeof(int));
        info.AddValue("something5", something5,typeof(bool));
        info.AddValue("something6", something6,typeof(double));
        info.AddValue("something7", something7,typeof(System.Drawing.Bitmap));
    }

然后是第二级:

[Serializable]
public class Third :ISerializable
{
  public String something8 { get; set; }
  public int something9 { get; set; }
  public System.Drawing.Bitmap something10 { get; set; }
  public First first { get; set; }
  public List<Second> second { get; set; }
}

和序列化/反序列化部分:

private Third(SerializationInfo info, StreamingContext context)
    {
        something8 = (String)info.GetValue("something8",typeof(String));
        something9 = (int)info.GetValue("HeroWins",typeof(int));
        something10 = (System.Drawing.Bitmap)info.GetValue("something10", typeof(System.Drawing.Bitmap));
        first = (First)info.GetValue("first", typeof(First));
        second = (List<Second>)info.GetValue("second", typeof(Second));
    }
public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
      info.AddValue("something8", something8,typeof(String));
      info.AddValue("something9", something9,typeof(int));
      info.AddValue("something10", something10, typeof(System.Drawing.Bitmap));
      info.AddValue("first", first, typeof(First));
      info.AddValue("second", second, typeof(List<Second>));
    }

最后是第三级:

[Serializable]
public class Fourth:ISerializable
{
     public String something11 { get; set; }
     public List<Third> third { get; set; }
}

和序列化/反序列化部分:

private Fourth(SerializationInfo info, StreamingContext context)
    {
        something11 = (String)info.GetValue("something11",typeof(String));
        third = (List<Hero>)info.GetValue("third", typeof(List<Third>));
    }
public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("something11", Name,typeof(String));
        info.AddValue("third", third,typeof(List<Third>));
    }

并最终错误发生的地方:D

public partial class MainForm : Form
{
   private List<Fourth> fourth; // this is initialized in Constructor on first time
   private Third t1, t2, t3; // these are initialized in Constructor on first time

   private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        using (FileStream str = File.Create("Third.bin"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(str, t1); 
            bf.Serialize(str, t2);
            bf.Serialize(str, t3);
        }

        using (FileStream str = File.Create("Fourth.bin"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(str, fourth);
        }
    }

    private void VersusGlavno_Load(object sender, EventArgs e)
    {
            using (FileStream str = File.OpenRead("Third.bin"))
            {
                BinaryFormatter bf = new BinaryFormatter();
                t1 = (Third)bf.Deserialize(str); //first TargetInvocationException, returns NULL
                t2 = (Third)bf.Deserialize(str); //second TargetInvocationException, returns NULL
                t3 = (Third)bf.Deserialize(str); //third TargetInvocationException, returns NULL
            }
            using (FileStream str = File.OpenRead("Fourth.bin"))
            {
                BinaryFormatter bf = new BinaryFormatter();
                fourth = null;
                fourth = (List<Fourth>)bf.Deserialize(str); //fourth TargetInvocationException, returns NULL
            }
    }

}

1 个答案:

答案 0 :(得分:1)

当反射尝试调用抛出异常的内容时,会发生

TargetInvocationException。为了更好地判断发生了什么,请发布

的详细信息
  

TargetInvocationException.InnerException