S3925: Why is explicit implementation of ISerializable + protected virtual implementation an incorrect implementation?

1,345 views
Skip to first unread message

fsat...@gmail.com

unread,
Jan 9, 2018, 7:48:36 AM1/9/18
to SonarLint
This class would trigger S3925:

[Serializable]
public class AClass: ISerializable
{

    public int Id { get; }

    public AClass(int id) {
         Id = id;
    }

    protected AClass(SerializationInfo info, StreamingContext context)
    {
        /* implementation here */
    }

    protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
         /* implementation here */
    }
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) => GetObjectData(info, context);
}

Converting the GetObjectData method to public removes the trigger. No need to remove the explicit ISerializable.GetObjectData.

AFAICT, the pattern I'm using is correct, as it allows deriving classes to extend GetObjectData; and has the benefit of not exposing GetObjectData to users because they really don't care. Is the rule buggy or my code wrong?

Saludos

valeri....@sonarsource.com

unread,
Jan 10, 2018, 3:20:01 AM1/10/18
to SonarLint
Hi, S3925 is one of our FxCop replacement rules, but it seems that the MSDN example for the corresponding CA2240 is exactly like your class, perhaps we missed something in our implementation:

I created a ticket for fixing the bug:


Reply all
Reply to author
Forward
0 new messages