私は現在、いくつかのレベルのクラスを持つ Windows フォーム アプリでシリアライゼーション/デシリアライゼーションに取り組んでいます。これをデバッグしようとして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));
}
次に、第 2 レベル:
[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>));
}
そして最後に 3 番目のレベル:
[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
}
}
}