3

所以这里有一个简单的测试用例:

[Serializable]
class Base
{

}

[Serializable]
class Derived : Base
{

}

BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, new Derived());

这里没什么特别的。如果您删除任何一个“可序列化”标签,BinaryFormatter就会因为这些项目不可序列化而对您大喊大叫。理论上,序列化 aDataTable也不应该工作,因为 DataTable 的基类 - 'MarshalByValueComponent- isn't marked as serializeable either ('typeof(MarshalByValueComponent).IsSerializable返回'false')。那么为什么 BinaryFormatter 忽略这个而不忽略其他不可序列化的类型呢?(或者为什么不MarshalByValueComponent标记为可序列化开始?)

4

1 回答 1

4

该类DataSet定义如下:

[SerializableAttribute]
public class DataSet : MarshalByValueComponent, IListSource, 
    IXmlSerializable, ISupportInitializeNotification, ISupportInitialize, ISerializable

请参阅:http: //msdn.microsoft.com/en-us/library/system.data.dataset.aspx

如您所见,有一个ISerializable接口的实现。该接口允许对象控制自己的序列化和反序列化。

于 2013-05-02T21:08:18.783 回答