2012-07-24 38 views
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属性时,我的代码崩溃了。

+1

如果你是新的序列化,你可能要考虑http://code.google的.com/p/protobuf网/。我发现比[Serialization]属性更容易使用。 – Liron 2012-07-24 20:02:35

+0

我觉得只使用''Serializable''要容易得多。您必须确保每个类与序列化类的关系都标有'[Serializable]'。 – 2012-07-24 20:26:25

+0

个人而言,我更喜欢XML序列化为二进制。我发现使用起来更容易,并且能够在事后查看原始数据,让您知道是否有问题。话虽如此,我承认有时候会走错路。我仍然更喜欢= D – Nevyn 2012-07-24 20:28:44

回答

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 
{ 
    ... 
}