Serializable Inheritance
If something inherits from a Serializable class, is the child class still Serializable?
It depends what you mean be serializable. If you mean the CLI marker (i.e. the [Serializable]
attribute), then this is not inherited (proof below). You must explicitly mark each derived class as [Serializable]
. If, however, you mean the ISerializable
interface, then yes: interface implementations are inherited, but you need to be careful - for example by using a virtual
method so that derived classes can contribute their data to the serialization.
using System;
class Program
{
static void Main()
{
Console.WriteLine(typeof(Foo).IsSerializable); // shows True
Console.WriteLine(typeof(Bar).IsSerializable); // shows False
}
}
[Serializable]
class Foo {}
class Bar : Foo {}