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 {}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…