Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 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
    }
}

[Serializable]
class Foo {}

class Bar : Foo {}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...