Rob P.
Rob P.

Reputation: 15071

Why Isn't My .Net Object Serializable?

I've got a 'MyDataTable' class that inherits from System.Data.DataTable

I've implemented ISerializable in my class and have a 'Public Overrides Sub GetObjectData...'

But when I try to serialize the an object of 'MyDataTable' I get an error saying that 'MyDataTable' is not marked as serializable.

If I used a DataTable instead - my code serializes correctly. If I add a serializable attribute to the 'MyDataTable' class - it serializes correctly, but I'm told that is unnecessary if I implement ISerializable.

Can someone point me in the right direction?

Upvotes: 6

Views: 4083

Answers (2)

Paul Kearney - pk
Paul Kearney - pk

Reputation: 5533

You need to add the SerializableAttribute, even if you implement ISerializable. Although this discusses an FxCop rule, this is from http://msdn.microsoft.com/en-us/library/ms182350(VS.80).aspx:

To be recognized by the common language runtime as serializable, types must be marked with the SerializableAttribute attribute even if the type uses a custom serialization routine through implementation of the ISerializable interface.

Upvotes: 1

Rex M
Rex M

Reputation: 144122

Whoever told you it is unnecessary to add the SerializableAttribute is incorrect:

Apply the SerializableAttribute attribute even if the class also implements the ISerializable interface to control the serialization process.

And from the ISerializable entry (em added):

Any class that might be serialized must be marked with the SerializableAttribute. If a class needs to control its serialization process, it can implement the ISerializable interface.

Upvotes: 8

Related Questions