Serializable Inheritance


If something inherits from a Serializable class, is the child class still Serializable?

10/4/2011 8:50:53 PM

Accepted Answer

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

class Foo {}

class Bar : Foo {}
11/20/2015 7:27:20 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow