开发者

C# Serialization/Inheritance [duplicate]

开发者 https://www.devze.com 2023-04-10 10:34 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: Serializable Inheritance
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

精彩评论

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

关注公众号