我试图序列化/反序列化包含Dictionary<Tuid,Section>的对象。这两个类型都是自定义类型。
在我的代码中,我有一个类型的模板,它包含Dictionary<Tuid,Section>。我试图序列化/反序列化的是模板类。
为了解决这个集合是字典的问题,我在模板类上实现了ISerializable接口.
[Serializable]
public class Template : ISerializable
{
protected Template(SerializationInfo info, StreamingContext context)
{
// Deserialize the sections
List<Tuid> tuids = (List<Tuid>)info.GetValue("Sections_Keys", typeof(List<Tuid>));
List<Section> sections = (List<Section>)info.GetValue("Sections_Values", typeof(List<Section>));
this._sections = new Dictionary<Tuid, Section>();
for (int i = 0; i < tuids.Count; i++)
{
_sections.Add(tuids[i], sections[i]);
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
List<Tuid> tuids = new List<Tuid>();
List<Section> sections = new List<Section>();
foreach (KeyValuePair<Tuid, Section> kvp in _sections)
{
tuids.Add(kvp.Key);
sections.Add(kvp.Value);
}
info.AddValue("Sections_Keys", tuids, typeof(List<Tuid>));
info.AddValue("Sections_Values", sections, typeof(List<Section>));
}这里的策略是将字典“解压缩”到两个独立的列表中,并将它们分别存储在序列化的流中。然后它们就会被重新创造出来。
我的科类也实现了ISerializable.
[Serializable]
public class Section : BaseObject
{
protected Section(SerializationInfo info, StreamingContext context):base(.....)
{
// Code
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// code
}
}问题是,当我序列化时,我的模板和我的部分都会调用GetObjectData(),这使我相信数据是可序列化的,并且它正在被序列化。
当我反序列化时,只调用模板上的反序列化构造函数。从未调用区段的反序列化构造函数。这样做的结果是,对info.GetValue("Section_Values"....)的调用确实返回一个列表,但其中包含一个项,该项为null。
为什么我的反序列化区段的构造函数从未被调用?可能是该部分中的某些数据无法序列化吗?如果是这样的话,如何找出它到底不能序列化什么?
更新:我刚才看到的一件事是,BaseObject for部分被标记为[Serializable],但没有实现ISerializable。
此外,我还想知道反序列化代码有多麻烦--它会针对同时构造基类的构造函数吗?
更新..
好的,我已经找到了这个问题,直到这个部分的序列化。代码看起来像这样..。
protected Section(SerializationInfo info, StreamingContext context):base(.....)
{
// Code
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
//info.AddValue("CustomObject", ClientInfo, typeof(CustomObject));
//info.AddValue("Description", Description, typeof(string));
}将这两行注释掉后,没有任何东西被序列化,反序列化构造函数在Section上被调用。如果我把字符串值加进去,一切都还好。但是,是的--您猜到了--如果我将CustomObject添加到序列化流中,则不调用反序列化构造函数。
请注意..。
deserialization.的反序列化构造函数
的基本构造函数
CustomObject不能是可序列化的,并且它的GetObjectData()方法运行良好,并且在deserialization.上构造得很好。
奇怪的是,纯粹是将这个可序列化的对象添加到流中,然后框架就会失败到Section的反序列化构造器!
为什么会发生这种事?
发布于 2011-12-01 11:26:52
我认为您的问题是因为序列化的工作方式(我似乎记得它的宽度优先,而不是深度优先)。这意味着它反序列化您的模板,然后在其中创建空间以添加到您的章节中。
然而,这些部分尚未反序列化,它们将在模板完成反序列化之后反序列化。您应该能够通过使用一个断点并让您的代码在更远的地方运行来检查它。解决这个问题的方法要么是oleksii的解决方案,要么类似于使用IDeserializationCallback。
当实例完成反序列化(以及它的成员)时,IDeserializationCallback是什么?它将调用一个方法,允许您进行一些连接。我会尝试下面这样的方法:
[Serializable]
public class Template : ISerializable, IDeserializationCallback
{
private List<Tuid> tuids;
private List<Section> sections
protected Template(SerializationInfo info, StreamingContext context)
{
tuids = (List<Tuid>)info.GetValue("Sections_Keys", typeof(List<Tuid>));
sections = (List<Section>)info.GetValue("Sections_Values", typeof(List<Section>));
this._sections = new Dictionary<Tuid, Section>();
}
public void OnDeserialization(object sender)
{
// Section serialization constructor should have been called by this point
for (int i = 0; i < tuids.Count; i++)
{
_sections.Add(tuids[i], sections[i]);
}
}
}https://stackoverflow.com/questions/8340098
复制相似问题