3

簡単なテストケースを次に示します。

[Serializable]
class Base
{

}

[Serializable]
class Derived : Base
{

}

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

ここでは特別なことは何もありません。「Serializable」タグのいずれかを削除するBinaryFormatterと、アイテムがシリアル化できないため、怒鳴られます。理論的には、DataTableDataTable の基本クラス - '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 に答える