Why do I get warning CA2229 (Implement serialization constructors) when inheriting from standard type

1.4k views Asked by At

I have a class definition like this:

[Serializable]
public class MyDictionary: Dictionary<string, object>
{
}

However, I get this Code Analysis warning:

CA2229 Implement serialization constructors Add a constructor to TcpFieldValueDictionary with the following signature: 'protected TcpFieldValueDictionary(SerializationInfo info, StreamingContext context)'.

However, the generic dictionary already has the constructor, only with a public modifier.

It is easy to add this constructor (see below), but why should this be done? What is the advantage?

    protected MyDictionary(SerializationInfo info, StreamingContext context)
        : base(info, context)
    { }
1

There are 1 answers

2
Damien_The_Unbeliever On BEST ANSWER

Constructors aren't inherited. It doesn't matter what constructors your base type has - if you want a constructor with a particular signature for your class, you have to implement one.

Otherwise, you just get the default parameterless constructor.