views:

133

answers:

2

I have a whole list of entity classes which I need to make Serializable (due to storing session state in SQL, but that's another story).

I have added the attribute [Serializable] and all seems to be fine.

All of my entity classes extend from the same base class. If I mark the base class as Serializable, does this mean all children are marked as Serializable too?

Thanks

+6  A: 

No, attribute is not inherited.

When you extend the class, it's possible to add features that might not be serializable by nature therefore .NET framework cannot assume for you that everything what extends serializable base class is also serializable.

That's why you must explicitly state [Serializable] attribute on every class individually.

lubos hasko
Thanks, that makes sense, and thanks for the prompt response :)
Russell
+2  A: 

Nope, each one will have to be marked as [Serializable] specifically.

Also if you intend to serialize an object to XML which is of a derived type as though it is the base type you'll also need a [XmlInclude] attribute.

EG:

[Serializable]
public class BaseClass : ParentClass
{
}

[Serializable]
[XmlInclude(typeof(BaseClass))]
public class ParentClass
{
}

(Binary serialization, like what is used for sessions, do not need this)

Tim Schneider
The serializing is done under the hood by ASP .net for the SQL Session state, so not sure if I need the XmlInclude, but thanks.
Russell
Ah, the XmlInclude is only for xml serialization (Which isn't used for session state). Must've missed that part when I read your question.
Tim Schneider