0

我是序列化的新手,所以很可能我遗漏了一些明显的东西!

我有一个名为 ResultsCollection 的类,它是一个四维数据结构——从 DataSet 类派生的对象的集合。每个修改后的 DataSet 都包含一个从 DataTable 派生的对象的集合。相关的代码位是:

[Serializable]
public class ResultsCollection : CollectionBase, ISerializable
{
  // indexer
  public MyDataSet this[int index] { get { return (MyDataSet)List[index]; } }
}
[Serializable]
public class MyDataSet : DataSet, ISerializable
{
  // member variable that *overrides* the Tables property of the standard DataSet class
  public new TablesCollection Tables;
}
[Serializable]
public class TablesCollection : CollectionBase, ISerializable
{
  // indexer
  public MyDataTable this[int index] { get { return (MyDataTable)List[index]; } }
}
[Serializable]
public class MyDataTable : DataTable, ISerializable
{
  ...
}

我已经通过包含一个公共序列化构造函数和一个公共 GetObjectData 函数来实现 ISerializable 接口,如下所示:

// ResultsCollection -- serialize all variables and also the inner list itself
public ResultsCollection(SerializationInfo info, StreamingContext ctxt) : base()
{
  _memberVariable = (bool)info.GetValue("_memberVariable", typeof(bool));
  ArrayList innerList = (ArrayList)info.GetValue("List", typeof(ArrayList));
  InnerList.AddRange(innerList);
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
  info.AddValue("_memberVariable", _memberVariable);
  info.AddValue("List", InnerList);
}

// MyDataSet -- call standard base-class (DataSet) serialization functions in addition to serializing members
public MyDataSet(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt)
{
  _memberVariable = (bool)info.GetValue("_memberVariable", typeof(bool));
  Tables = (TablesCollection)info.GetValue("Tables", typeof(TablesCollection));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
  base.GetObjectData(info, ctxt);
  info.AddValue("_memberVariable", _memberVariable);
  info.AddValue("Tables", Tables);
}

// TablesCollection -- serialize all variables and also the inner list itself
public TablesCollection(SerializationInfo info, StreamingContext ctxt) : base()
{
  _memberVariable = (bool)info.GetValue("_memberVariable", typeof(bool));
  ArrayList innerList = (ArrayList)info.GetValue("List", typeof(ArrayList));
  InnerList.AddRange(innerList);
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
  info.AddValue("_memberVariable", _memberVariable);
  info.AddValue("List", InnerList);
}

// MyDataTable -- call standard base-class (DataTable) serialization functions in addition to serializing members
public MyDataSet(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt)
{
  _memberVariable = (bool)info.GetValue("_memberVariable", typeof(bool));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
  base.GetObjectData(info, ctxt);
  info.AddValue("_memberVariable", _memberVariable);
}

我有一个 ResultsCollection 对象,我想将它存储在 ViewState 中,以便在下一次回发时检索它(因此所有这些麻烦)。对于最初的请求,事情似乎正在发挥作用:我在序列化函数中设置了断点,并且数据成员确实正在被序列化。但是,在反序列化时,成员变量被正确加载,但 List 对象充满了空成员。当我尝试从 ResultsCollection 中的第一个 MyDataSet 对象检索 DataSetName 属性时,我的代码崩溃了。

4

1 回答 1

0

事实证明CollectionBase 和 ISerializable 是不兼容的,可能是因为 CollectionBase 的 InnerList 属性是只读的。因此,我需要做的是从两个 CollectionBase 派生类中删除ISerializable 定义,即:

[Serializable]
public class ResultsCollection : CollectionBase
{
  // indexer
  public MyDataSet this[int index] { get { return (MyDataSet)List[index]; } }
}
[Serializable]
public class MyDataSet : DataSet, ISerializable
{
  // member variable that *overrides* the Tables property of the standard DataSet class
  public new TablesCollection Tables;
}
[Serializable]
public class TablesCollection : CollectionBase
{
  // indexer
  public MyDataTable this[int index] { get { return (MyDataTable)List[index]; } }
}
[Serializable]
public class MyDataTable : DataTable, ISerializable
{
  ...
}
于 2012-07-27T09:37:03.747 回答