bobbo
bobbo

Reputation: 865

Serialization and attributes

When should I mark a class as serializable with the [Serializable] attribute? The documention says that the class should not be inherited. However, I have used this attribute in conjuction with the XmlInclude attribute so I can serialize my derived classes as well. Is this incorrect?

I am using the XmlSerializer and StreamWriter classes to serialize my objects.

Also, my understanding tells me that any public properties (get and sets) will be serialized, other fields will be ignored. Is this also correct?

Any guidance would be appreciated.

Thanks

Upvotes: 3

Views: 438

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1064204

XmlSerializer doesn't check this flag; the answer is "when using BinaryFormatter (or a few others; not many) and not implementing ISerializable".

Most serializers don't use this flag.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292735

When should I mark a class as serializable with the [Serializable] attribute? The documention says that the class should not be inherited. However, I have used this attribute in conjuction with the XmlInclude attribute so I can serialize my derived classes as well. Is this incorrect?

I am using the XmlSerializer and StreamWriter classes to serialize my objects.

You never need the [Serializable] attribute for XML serialization. It's only used for serialization with formatters (e.g. BinaryFormatter, SoapFormatter)

Also, my understanding tells me that any public properties (get and sets) will be serialized, other fields will be ignored. Is this also correct?

This is correct. Only public read/write properties are serialized, not fields.

Upvotes: 2

Related Questions