如何序列化派生数据集的集合

时间:2012-07-24 19:58:19

标签: c# serialization collections dataset

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

我有一个名为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);
}

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

1 个答案:

答案 0 :(得分: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
{
  ...
}