开发者

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 开发者_JAVA百科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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜