Why Isn't My .Net Object Serializable?

偶尔善良 提交于 2019-12-22 05:36:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!