Is there a way to just tell the compiler, that I want my objects to be serializable by default?
Asked
Active
Viewed 2,476 times
2 Answers
3
Pretty much every serialization engine is going to want to know that your objects are suitable. This can take the form of:
[Serializable]
/ISerializable
(BinaryFormatter
,SoapFormatter
)[Serializable]
/IXmlSerializable
(andpublic
) (XmlSerializer
)[DataContract]
/[MessageContract]
(or most of the above) (DataContractSerializer
and variants)
AFAIK, there is no way of avoiding this step.

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
1can you explain a scenario when you wouldn't mark an object serializable? – DevelopingChris Nov 21 '10 at 14:31
-
1@DevelopingChris - when that isn't a requirement, so hasn't been tested properly. – Marc Gravell Nov 21 '10 at 23:50
1
No, you need to decorate the objects with the Serializable attribute. FYI all objects are Xml Serializable by default. The Xml Serializer doesn't require the attribute.
I'm not sure about DataContracts but last I checked you needed to decorate them as well.

JoshBerke
- 66,142
- 25
- 126
- 164