的BinaryFormatter和反序列化复杂对象 [英] BinaryFormatter and Deserialization Complex objects
问题描述
无法反序列化对象图如下。当呼吁BinaryFormmater Deserialize方法发生该异常:
System.Runtime.Serialization.SerializationException:
反序列化类型'C'的对象的构造函数没有被发现。
这里有两种构造函数C.我认为这个问题可能是:虽然序列化的BinaryFormatter使用paramatered之一,并在反序列化过程,它需要一个无参数之一。是否有黑客/解决方案?
对象:
[Serializable接口]
大众A级
{
B B;
C C; 公众诠释ID {搞定;组; } 大众A()
{
} 大众A(B B)
{
this.b = B;
} 大众A(C C)
{
this.c = C;
}
}
[Serializable接口]
大众B级
{ }
[Serializable接口]
公共类C:字典< INT,A>
{
市民C()
{ } 市民C(名单< A>清单)
{
list.ForEach(p值=> this.Add(p.ID,P));
}
}
//序列化的成功
字节[]的结果;
使用(VAR流=新的MemoryStream())
{
新的BinaryFormatter().Serialize(流源);
stream.Flush();
结果= stream.ToArray();
}
返回结果;
//反序列化失败
对象result = NULL;
使用(VAR流=新的MemoryStream(缓冲))
{
结果=新的BinaryFormatter().Deserialize(流);
}
返回结果;
电话是在相同的环境,相同的线程,同样的方法
列表< A> ALIST =新的List< A>()
{
新A {n = 1},
新A {ID = 2}
}; C C =新C(ALIST);
VAR牵强=序列化(C); //成功
VAR OBJ =反序列化(取); // failes
我怀疑你只需要提供一个反序列化的构造函数 C
,因为字典工具 ISerializable的
:
保护C(的SerializationInfo信息,的StreamingContext CTX):基地(信息,CTX){}
检查(通行证):
静态无效的主要(){
C C =新C();
c.Add(123,新A {ID = 456});
使用(VAR毫秒=新的MemoryStream()){
VAR SER =新的BinaryFormatter();
ser.Serialize(MS,C);
ms.Position = 0;
C克隆=(C)ser.Deserialize(MS);
Console.WriteLine(clone.Count); //写1
Console.WriteLine(克隆[123] .ID); //写456
}
}
Can not deserialize following object graph. That Exception occurs when deserialize method called on BinaryFormmater: System.Runtime.Serialization.SerializationException :
The constructor to deserialize an object of type 'C' was not found.
There're two constructor on C. and I think the problem may be : While serialization Binaryformatter using the paramatered one and on deserialization process, it needs a parameterless one. Is there a hack / solution? Objects :
[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));
}
}
// Serialization success
byte[] result;
using (var stream =new MemoryStream())
{
new BinaryFormatter ().Serialize (stream, source);
stream.Flush ();
result = stream.ToArray ();
}
return result;
// Deserialization fails
object result = null;
using (var stream = new MemoryStream(buffer))
{
result = new BinaryFormatter ().Deserialize (stream);
}
return result;
The calls are at the same environment, same thread, same method
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
I suspect you just need to provide a deserialization constructor to C
, since dictionary implements ISerializable
:
protected C(SerializationInfo info, StreamingContext ctx) : base(info, ctx) {}
checked (passes):
static void Main() {
C c = new C();
c.Add(123, new A { ID = 456});
using(var ms = new MemoryStream()) {
var ser = new BinaryFormatter();
ser.Serialize(ms, c);
ms.Position = 0;
C clone = (C)ser.Deserialize(ms);
Console.WriteLine(clone.Count); // writes 1
Console.WriteLine(clone[123].ID); // writes 456
}
}
这篇关于的BinaryFormatter和反序列化复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!