Why is warning CA2214 not resolved when a sealed override method is generic?

馋奶兔 提交于 2019-12-13 01:27:55

问题


Given the following class heirarchy:

class Base
{
    protected virtual void Do(int value)
    {
    }
}

class Derived1 : Base
{
    sealed protected override void Do(int value)
    {
        base.Do(value);
    }
}

class Derived2 : Derived1
{
    public Derived2()
    {
        Do(999);
    }
}

... the code analysis warning CA2214 is resolved by simply adding the sealed keyword to Derived1.Do(). So far, so good.

Now let's make Do() generic:

class Base
{
    protected virtual void Do<T>(T value)
    {
    }
}

class Derived1 : Base
{
    sealed protected override void Do<T>(T value)
    {
        base.Do(value);
    }
}

class Derived2 : Derived1
{
    public Derived2()
    {
        Do(999);
    }
}

The CA2214 warning returns. Why?

The description of the warning cites the following call stack to review:

Derived2..ctor()
Base.Do<T>(T):Void

...even though a breakpoint on Derived1.Do() is hit just fine.

Note: this is the case with both .NET 4.5 and 4.6


回答1:


The rule implementation does not implement parameter matching logic that recognizes invocations of methods with generic parameters. This is most likely a flaw in the rule implementation, as opposed to an intentional exclusion of generic methods from the sealing solution. You may want to consider suppressing the violation as a false positive. However, that suppression would cause a potential future unsealing of the method to go undetected, so YMMV...



来源:https://stackoverflow.com/questions/32125773/why-is-warning-ca2214-not-resolved-when-a-sealed-override-method-is-generic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!