为什么我的.NET对象序列化? [英] Why Isn't My .Net Object Serializable?

查看:105
本文介绍了为什么我的.NET对象序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个'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?

推荐答案

谁告诉你这是没有必要添加<一href="http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx">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.

和从<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx">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.

这篇关于为什么我的.NET对象序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆