当密封的覆盖方法是通用的时,为什么警告CA2214无法解决? [英] Why is warning CA2214 not resolved when a sealed override method is generic?
问题描述
给出以下类层次结构:
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);
}
}
...代码分析警告 CA2214 的解决方法是,只需添加 sealed
关键字即可到 Derived1.Do()
。到目前为止,一切都很好。
... the code analysis warning CA2214 is resolved by simply adding the sealed
keyword to Derived1.Do()
. So far, so good.
现在让我们将 Do()
泛型化:
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);
}
}
CA2214警告返回。为什么?
The CA2214 warning returns. Why?
警告的描述引用了以下调用堆栈以供查看:
The description of the warning cites the following call stack to review:
Derived2..ctor()
Base.Do<T>(T):Void
...即使断点位于 Derived1.Do()
恰好被击中。
...even though a breakpoint on Derived1.Do()
is hit just fine.
注意:.NET 4.5和4.6都是这种情况
Note: this is the case with both .NET 4.5 and 4.6
推荐答案
规则实现未实现用于识别具有通用参数的方法调用的参数匹配逻辑。与有意从密封解决方案中排除通用方法相反,这很可能是规则实施中的缺陷。您可能需要考虑将违规作为误报抑制。但是,这种抑制将导致该方法将来可能无法启封,因此YMMV ...
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...
这篇关于当密封的覆盖方法是通用的时,为什么警告CA2214无法解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!