What does this [Serializable] attribute that ISERializable does not support?

Possible duplicate:
What is the difference between using the Serializable attribute and implementing ISerializable?

What puts a class with Serializable to do? eg:.

 [Serializable] public Hashtable { } 

How is it different from a class that implements ISerializable ? eg:.

 public Hashtable : ISerializable { } 

And how does this differ from marking a class as Serializable and implement ISerializable ? eg:.

 [Serializable] public Hashtable : ISerializable { } 

What is the purpose of [Serializable] and not ISerializable ?

tl; dr : What is [Serializable] ?

+4
source share
2 answers

I thought you contacted Serializable , but you did not:

All public and private fields in a type marked with SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process.

and

Apply the SerializableAttribute attribute, even if the class also implements the ISerializable interface [...]

That is, the Serializable attribute indicates that this type can be serialized. ISerializable indicates that this type wants to control how this type is serialized.


Or, in other words, your question is not formulated correctly. SerializableAttribute must always be applied (to serializable classes) and is the "base" level of serialization. ISerializable adds more (letting you write code to control the process).

+14
source

ISerializable means you need to override / implement the void GetObjectData method

The [Serializable] directive lets the compiler know that this class can be serialized. And the CLR throws an exception if the class does not meet the requirements: that is, the default constructor, unknown types, etc.

+4
source

Source: https://habr.com/ru/post/1387447/


All Articles