views:

169

answers:

5

Seeing as you can convert any document to a byte array and save it to disk, and then rebuild the file to its original form (as long as you have meta data for its filename etc.).

Why do you have to mark a class with [Serializable] etc? Is that just the same idea, "meta data" type information so when you cast the object to its class things are mapped properly?

+2  A: 

It indicates to the serializer that you want that class to be serialized as you may not want all properties or classes to be serialized.

Chris
It can only be applied to types, not members.
Lasse V. Karlsen
Woops, my mistake - answer corrected
Chris
+8  A: 

First off, you don't have to.

It is simply a marker interface that tells the serializer that the class is composed of items that it can serialize (which may or may not be true) and that is can use the default serialization.

The XMLSerializer has the additional requirement to have a zero parameter constructor to the class.

There are other serializers that use contracts for serialization (such as the DataContractSerializer) - they give you more control over serialization than simply marking a class as Serializable. You can also get more control by implementing the ISerializable interface.

Oded
The XmlSerializer does not require SerializableAttribute
Matthew Whited
@Oded: -1 until you edit your answer to show that the `XmlSerializer` doesn't care about `[Serializable]`.
John Saunders
@Matthew Whited, @John Saunders - thanks for the correction. Answer updated.
Oded
Just chiming in to recommend the chapter about serializing in "Framework Design Guidelines" by Krzysztof Cwalina and Brad Abrams which explains a bit when to use what from the standpoint of a Framework designer.
Michael Stum
A: 

It's basically metadata that indicates that a class can be serialized, nothing more.

It is required by a lot of framework serializers, which refuse to deal with types not having this attribute applied to them.

Lasse V. Karlsen
A: 

Serialization can create security holes and may be plagued by versioning problems. On top of that, for some classes, the very idea of serialization is outright nonsense.

For details, see the excellent answers to Why Java needs Serializable interface?, especially this one, this one, and this one. They make the case that serialization should be a feature you have to explicitly opt into.

For a counterpoint, the accepted answer to that question makes the case that classes should be serializable by default.

Jeff Sternal
+2  A: 

Binary serialization is pretty powerful, it can set fields in your class that you declared private. Regular code cannot do this. By applying the [Serializable] attribute, you explicitly give it the go-ahead to mess with your private parts.

XML serialization doesn't need this kind of okay, it only serializes members that are public.

DataContractSerializer can serialize private members as well. It therefore needs an explicit okay again, now with the [DataContract] attribute.

Hans Passant
+1 for messing with your private parts!
Dan Diplo