Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Serialization/Inheritance [duplicate]

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 marked as such?

like image 275
zimdanen Avatar asked Oct 04 '11 20:10

zimdanen


1 Answers

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  {  } 
like image 75
Jordão Avatar answered Oct 30 '22 04:10

Jordão