我正在尝试对包含 Dictionary<Tuid,Section>
的对象进行序列化/反序列化。这两者都是自定义类型。
在我的代码中,我有一个名为模板(Template)的类型,它包含Dictionary<Tuid,Section>
。 我正试图对 模板 类进行序列化/反序列化。
为了解决此集合是字典的问题,我在我的 Template 类上实现了 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>));
}
这里的策略是将字典“展开”为两个单独的列表,并将它们分别存储在序列化流中。然后在之后重新创建它们。
我的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"....)
的调用返回一个List,但其中只有一个null项。为什么我的反序列化部分的构造函数从未被调用?它可能是由于部分内部的某些数据不能序列化吗?如果是这样,如何找出确切不能序列化的内容?
更新:我刚刚发现的一件事是,部分的BaseObject标有
[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
添加到序列化流中,则不会调用反序列化构造函数。
请注意...
Section
的反序列化构造函数是一个空方法 - 我不尝试对反序列化数据进行任何操作。- Section 的基本构造函数已被打桩以传递新的有效对象,并且我已确认这可以正常运行。
- 没有抛出任何异常告诉我无法序列化
CustomObject
。 CustomObject
是可序列化的,其GetObjectData()
方法可以正常运行,并且在反序列化时它也被正确构造。
纯粹通过将此可序列化对象添加到流中,框架就无法将其反序列化到 Section
的反序列化构造函数中,这似乎很奇怪!!
为什么会发生这种情况呢?