同様の型付けされた子のリストを含む C# クラスに ISerializable を実装したいと思います。次の例を検討してください。
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace serialisation
{
[Serializable]
internal class Nested : ISerializable
{
public string Name { get; set; }
public List<Nested> Children { get; set; }
public Nested(string name)
{
Name = name;
Children = new List<Nested>();
}
protected Nested(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
Name = info.GetString("Name");
// This doesn't work:
Nested[] children = (Nested[])info.GetValue("Children", typeof(Nested[]));
Children = new List<Nested>(children);
// This works:
// Children = (List<Nested>)info.GetValue("Children", typeof(List<Nested>));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("Name", Name);
// This doesn't work:
info.AddValue("Children", Children.ToArray());
// This works:
//info.AddValue("Children", Children);
}
}
internal class Program
{
private static void Main(string[] args)
{
// Generate a hierarchy
Nested root = new Nested("root");
Nested child1 = new Nested("child1");
Nested child2 = new Nested("child2");
Nested child3 = new Nested("child3");
child1.Children.Add(child2);
child1.Children.Add(child3);
root.Children.Add(child1);
Nested deserialized;
BinaryFormatter binaryFmt = new BinaryFormatter();
// Serialize
using (var fs = new FileStream("Nested.xml", FileMode.OpenOrCreate))
{
binaryFmt.Serialize(fs, root);
}
// Deserialize
using (var fs = new FileStream("Nested.xml", FileMode.OpenOrCreate))
{
deserialized = (Nested)binaryFmt.Deserialize(fs);
}
// deserialized.Children contains one null child
Console.WriteLine("Original Name: {0}", root.Name);
Console.WriteLine("New Name: {0}", deserialized.Name);
}
}
}
上記のサンプルでは、Nested.GetObjectData と Nested のシリアライザー コンストラクターが 4 回、次々に呼び出されます。
ネストされた配列として子をシリアライザーに追加すると、デシリアライズ時に正しいサイズの配列が返されますが、すべての要素が null になります。
ただし、ネストされた配列からネストされたリストにタイプを変更すると、子のコンストラクターが呼び出された後に null 要素が魔法のように修正されます。
私が知りたいのは:
- ネストされたリストの特別な点は何ですか?
- このような再帰構造を持つクラスをシリアライズするための推奨される方法は何ですか?
アップデート:
逆シリアル化が行われた後に呼び出される追加のインターフェイス、IDeserializationCallback.OnDeserialization があるようです (呼び出し順序は非決定論的です)。逆シリアル化された配列をコンストラクターの一時メンバー変数に格納し、このメソッドでリストに割り当てることができます。何かが欠けていない限り、実装を一時変数で混乱させる必要があるため、これは理想的とは言えません。