28

I am looking at somebody elses C# code and before a public enum there are the following lines:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]

Can somebody explain in plain english what the SerializableAttribute line are does?

I already came across this page - it didn't make much sense to me - I'm new at C#.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Graham
  • 7,807
  • 20
  • 69
  • 114

4 Answers4

75

This is actually quite subtle...

On the surface, the answer is simply "it adds the SerializableAttribute to the metadata for the class", where the purpose of SerializableAttribute is to advertise (to things like BinaryFormatter) that a type can be serialized. BinaryFormatter will refuse to serialize things that aren't explicitly advertised for serialization. This may be a consequence of BinaryFormatter being used to implement remoting, and to prevent data accidentally leaking across a remoting boundary.

Note that most serializers don't care about SerializableAttribute, so this only impacts things like BinaryFormatter. For example, none of XmlSerializer, DataContractSerializer, JavaScriptSerializer, JSON.NET or protobuf-net really care about SerializableAttribute.

However, actually, it is not a standard attribute, but has special handling by the compiler:

  • most attributes are technically .custom instance values (in IL terms)
  • however, SerializableAttribute actually maps to a CLI .class flag, serializable

This doesn't change the meaning, but : as a novelty fact, SerializableAttribute is not actually implemented as an attribute.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    Marc can you please elaborate on the IL part? What exactly does the change mean, and why does this attribute is handled differently? Thanks – gdoron May 13 '16 at 00:17
13

The System.SerializableAttribute specifies to the runtime that the instances of that class can be serialized

Eg. You return an object in a WCF service call. If that object has this attribute and all of the objects inside it are serializable, the runtime will transform that object to JSON or XML, depending on the type of resource the web service returns.

  • Cheers - so basically, I can ignore it as it doesn't affect how I am using it. – Graham Sep 17 '12 at 14:32
  • 6
    WCF typically uses `DataContractSerializer`; `DataContractSerializer` doesn't actually need `[Serializable]` - it prefers `[DataContract]` – Marc Gravell Sep 17 '12 at 14:32
7

This is just long-hand for [Serializable]. It tags the class as one that can be 'converted' to and from other formats.

Common examples include 'Serialization' of a class 'to' a JSON or XML data structure, and also the equivalent conversion 'from' such structures.

Consider a class

[Serializable]
class MyClass
{
    public string Mem1 {get; set;}
    public string Mem2 {get; set;}
}

...
MyClass mc = new MyClass;
mc.Mem1 = "Hello";
mc.Mem2 = "World";

When serialised to a JSON structure, we get:

"{'Mem1':'Hello','Mem2':'World'}"

And given the two-way nature of the process, if we recieved information of this format (e.g. back from some web service), then we could happily Serialize it back into an instance of this class.

Building on the JSON example, we find a series of classes in the namespace System.Web.Script.Serialization that can help us with this. In particular, the JavaScriptSerializer class helps us with the provision of Serialize() and Deserialize methods.

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • 4
    Your examples of JSON or XML would typically use JavaScriptSerializer, DataContractSerializer, XmlSerializer or JSON.NET; not one of which cares about `[Serializable]`. – Marc Gravell Sep 17 '12 at 14:35
  • True, if indeed we could guarantee that it would only ever be serialized in this way. – James Wiseman Sep 17 '12 at 14:38
-1

From the documentation:

Indicates that a class can be serialized

See the documentation for an example.

larsmoa
  • 12,604
  • 8
  • 62
  • 85