如何满足CA2202(请勿多次放置对象) [英] How to satisfy CA2202 (Do not dispose objects multiple times)

查看:72
本文介绍了如何满足CA2202(请勿多次放置对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这个问题在您看来似乎是 CA2202的重复项,如何解决这种情况,它的答案已被接受。但是您可能会意识到,基于质量较差,已接受的答案有5票赞成。同样,任何其他已投票表决的答案实际上也不能解决问题。他们中的大多数人解释了如何压制该规则,或者就该规则有多错误以及为什么我们应该忽略它进行了辩论。既然有了该规则,就应该有一种方法可以满足它,我正在寻找社区支持来解决该问题。

This question may seem to you duplicate of CA2202, how to solve this case which has an accepted answer. But you may realize that accepted answer has 5 down votes based on poor quality. Also any other up voted answers are not actually solves the issue. Most of them explains how to suppress the rule or debates about how wrong this rule is and why we should ignore it. Since that rule is there, there should be a way to satisfy it and I'm looking for community support to solve that issue.

我正在尝试找出如何在以下代码中满足 CA2202 的要求。我了解这里的问题是 using 语句还处理了 encryptedStream 对象。但是,如果我删除了 finally 部分,它将开始抛出 CA2000

I'm trying to figure it out how to satisfy CA2202 in the following code. I understand that the issue here is, using statement also disposes the encryptedStream object. But if I remove the finally part, it starts to throw CA2000

那么,编写符合CA2202和CA2000的正确方法是什么?

So, what is the correct way of writing it to comply with CA2202 and CA2000

byte[] result;

MemoryStream encryptedStream = null;
try
{
    encryptedStream = new MemoryStream();
    using (var cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write))
    {
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
        cryptStream.FlushFinalBlock();
        result = encryptedStream.ToArray();
    }
}
finally
{
    encryptedStream?.Dispose();
}
string output = Convert.ToBase64String(result);


推荐答案

这是对您的问题的字面回答,因为它将不发出CA警告而不会抑制它们,并且只会对每个 Dispose 调用一次:

This is a literal answer to your question, in that it will not issue CA warnings without suppressing them, and will only ever call every Dispose once:

MemoryStream encryptedStream = null;
CryptoStream cryptStream = null;
try {
    encryptedStream = new MemoryStream();
    cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
    cryptStream.FlushFinalBlock();
    result = encryptedStream.ToArray();
} finally {
    if (cryptStream != null) {
        cryptStream.Dispose();
   } else {
        if (encryptedStream != null) encryptedStream.Dispose();
   }
}
string output = Convert.ToBase64String(result);

但是任何值得他们投入盐分的开发人员都应该看一下,然后说嗯,就像他们没有不知道使用 ,我最好重写一下。 请勿在生产代码中执行此操作。抑制警告。获得这样正确的代码(并使其在更改时保持正确)实际上比编写使用 using 。 c $ c>带有虚假警告的抑制(实际上,我不确定上面的代码 是正确的!)。首先,它消除了进行静态代码分析的全部要点:编写可靠的代码。您应该将代码分析视为一种工具,而不是正确性的仲裁者。

But any developer worth their salt should take a look at this and go "hmm, it's like they didn't know using, I'd better rewrite that". Do not do this in production code. Suppress the warning. Getting code like this correct (and having it remain correct in the face of changes) is actually harder than writing code that uses using with suppression of spurious warnings (indeed, I'm not entirely sure the above code is correct!). It defeats the entire point of having static code analysis in the first place: to write reliable code. You should see code analysis as a tool, not an arbiter of correctness.

这篇关于如何满足CA2202(请勿多次放置对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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