开发者

C# Serialization/Inheritance [duplicate]

This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

Serializable Inheritance

Is serialization inheritable?

Example:

[Serializable]
class A
{
}

class B : A
{
}

If I try to serialize/deserialize an instance of class B, I get an exception stating that the class is not marked as serializable. Thus the question: is serialization inheritable? Am I just missing how to do it, or does every class that needs to be serialized require explicitly being m开发者_运维知识库arked as such?


It's not inheritable:

It is important to note that the Serializable attribute cannot be inherited. If you derive a new class from MyObject, the new class must be marked with the attribute as well, or it cannot be serialized.

You should mark the subclass as well to make it serializable:

[Serializable]
class B : A  {  } 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜