当使用CA2213警告? (空条件运算符)调用Dispose [英] CA2213 warning when using ?. (null-conditional Operator) to call Dispose

查看:733
本文介绍了当使用CA2213警告? (空条件运算符)调用Dispose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的IDisposable ,并在我的的Dispose()时调用方法的Dispose()其他管理的资源我使用 运营商,像这样:

I'm implementing IDisposable, and in my Dispose() method when calling Dispose() on other managed resources I'm using the ?. operator like so:

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if(disposing)
        {
            _ChangeLock?.Dispose();
        }
    }



我仍然得到以下的代码分析错误

CA2213:'MyClass的'包含字段'MyClass._ChangeLock这是IDisposable的类型:ReaderWriterLockSlim。改变'MyClass的'Dispose方法调用Dispose或在此领域的关闭。

CA2213: 'MyClass' contains field 'MyClass._ChangeLock' that is of IDisposable type: 'ReaderWriterLockSlim'. Change the Dispose method on 'MyClass' to call Dispose or Close on this field.

如果我更改为标准零检查,代码分析警告消失:

If I change to a standard null check, the code analysis warning goes away:

if(_ChangeLock != null)
    _ChangeLock.Dispose();



时有什么错用空条件运算符我的方式,或者是这个代码分析规则已经过时,或者是什么?

Is there something wrong with using the null-conditional operator the way I am, or is this code analysis rule outdated, or what?

推荐答案

这是一个的已知问题用的FxCop

This is a known issue with FxCop.

看来他们的决定不修复的:

我们决定削减[CA2213],因为它是很难得到它的权利没有更深入的分析,跟踪变量,可能有注释。当前的实现是很嘈杂,有很多误报和规则的价值是不值得所有它产生的噪音。

We decided to cut [CA2213] because it is very hard to get it right without deeper analysis, tracking variables and possibly having annotations. The current implementation is very noisy and has a lot of false positives and the value of the rule is not worth all the noise it generates.

这篇关于当使用CA2213警告? (空条件运算符)调用Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆