次のオブジェクト グラフをデシリアライズできません。その例外は、BinaryFormmater: System.Runtime.Serialization.SerializationException で呼び出された deserialize メソッドで発生します。
The constructor to deserialize an object of type 'C' was not found.
C.には2つのコンストラクターがあり、問題は次のようになると思います:パラメーター付きのものと逆シリアル化プロセスを使用するシリアライゼーション Binaryformatter では、パラメーターなしのコンストラクターが必要です。ハック/解決策はありますか? オブジェクト:
[Serializable]
public class A
{
B b;
C c;
public int ID { get; set; }
public A()
{
}
public A(B b)
{
this.b = b;
}
public A(C c)
{
this.c = c;
}
}
[Serializable]
public class B
{
}
[Serializable]
public class C : Dictionary<int, A>
{
public C()
{
}
public C(List<A> list)
{
list.ForEach(p => this.Add(p.ID, p));
}
}
// シリアライズ成功
byte[] result;
using (var stream =new MemoryStream())
{
new BinaryFormatter ().Serialize (stream, source);
stream.Flush ();
result = stream.ToArray ();
}
return result;
// デシリアライズ失敗
object result = null;
using (var stream = new MemoryStream(buffer))
{
result = new BinaryFormatter ().Deserialize (stream);
}
return result;
呼び出しは同じ環境、同じスレッド、同じメソッドにあります
List<A> alist = new List<A>()
{
new A {ID = 1},
new A {ID = 2}
};
C c = new C(alist);
var fetched = Serialize (c); // success
var obj = Deserialize(fetched); // failes