Impact of not marking a class <Serializable ()>

You can mark a class as serializable using SerializableAttribute . However, you can still serialize an object without marking it as serializable. What is the effect of this?

+3
source share
3 answers

I assume that when you say, "You can still serialize an object without labeling it serializable," you mean that you are serializing / deserializing yourself.

Adding the [Serializable] attribute indicates third-party code that the object can be serialized. This is especially useful if you want to save an object in an ASP.NET session or in another tool (for example, in the Memcached cache).

+2
source

Putting [Serializable]in a type allows you to use it with APIs [System.Runtime.Serialization][1]that are very convenient for the most common scenarios and save you from writing a lot of repeated code to serialize / deserialize your objects, which is usually cumbersome to test and error prone. And they get some flexibility in terms of basic storage mechanisms (e.g. Binary, XML, SOAP ...).

[Serializable] , , / , , ISerializble . , , , .

, , Serializable , ( ), , , , , Serializable.

+1

As I understand it, you can use a surrogate to serialize a non-serializable object, but what would prompt you to do this? I do not think that a private member will be serialized using this approach. You probably need a default constructor, etc.

0
source

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


All Articles