为什么我的.NET对象序列化?我的、对象、序列化、NET

2023-09-03 16:32:49 作者:你是我的风景

我有一个'MyDataTable类,它继承System.Data.DataTable

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

我在我的类中实现了ISerializable,并有一个公共覆盖子GetObjectData使用......

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

但是,当我试图序列化的对象MyDataTable我得到一个错误说MyDataTable'未标记为可序列化。

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

如果我用一个数据表,而不是 - 我的code序列化正确。 如果我添加一个可序列化属性的MyDataTable类 - 它系列化正确,但有人告诉我这是不必要的,如果我实现了ISerializable

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?

推荐答案

谁告诉你这是没有必要添加SerializableAttribute是不正确的:

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

应用SerializableAttribute属性,即使该类还实现ISerializable接口来控制序列化进程。

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

和从ISerializable项( EM 补充):

这可能是序列化的任何类都必须标有Seri​​alizableAttribute 。如果一个类需要控制它的序列化过程中,它可以实现ISerializable接口。

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.