开发者

If Base class is marked Serializable are all child classes marked too?

I have a whole list of entity classes which I need to make Serializ开发者_Python百科able (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


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜