Search code examples
.netattributesserializable

What is the CLR doing "automagically" when a type is annotated [serializable]?


I understand that when you annotate a type with [serializable] it tells the CLR that this type can be serialized.

I don't like this seeming like "black box" magic to me. I want to know what it does when I mark it with this attribute.

In addition it would be helpful to know what it then does when I actually try to serialize an object.

P.S. if the answer to this question can pertain to the handling of any attribute, please feel free to edit my question title and question to reflect this. Thanks.


Solution

  • The Common Language Runtime (CLR) manages how objects are laid out in memory and the .NET Framework provides an automated serialization mechanism by using reflection. When an object is serialized, the name of the class, the assembly, and all the data members of the class instance are written to storage. Objects often store references to other instances in member variables. When the class is serialized, the serialization engine keeps track of all referenced objects already serialized to ensure that the same object is not serialized more than once. The serialization architecture provided with the .NET Framework correctly handles object graphs and circular references automatically. The only requirement placed on object graphs is that all objects referenced by the object that is being serialized must also be marked as Serializable. If this is not done, an exception will be thrown when the serializer attempts to serialize the unmarked object.

    this part of this article: http://msdn.microsoft.com/en-us/library/ms973893.aspx