views:

57

answers:

2

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?

+6  A: 

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.

Rex M
Thank you for your quick response. My testing seems to support that and examples I've found in the MSDN seem to agree as well.
Rob P.
+1  A: 

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.

Paul Kearney - pk