问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/2961931/why-isnt-my-net-object-serializable