Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 676
Rob P. Avatar asked Jun 02 '10 22:06

Rob P.


People also ask

Is object serializable C#?

Serialization in C# is the process of converting an object into a stream of bytes to store the object to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How do you determine if the given object is serializable?

You can determine whether an object is serializable at run time by retrieving the value of the IsSerializable property of a Type object that represents that object's type.

What is .NET serialization?

.Net Serialization (C#/VB.Net) Serialization can be defined as the process of converting the state of an object instance to a stream of data, so that it can be transported across the network or can be persisted in the storage location.


1 Answers

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.

like image 181
Rex M Avatar answered Oct 23 '22 12:10

Rex M