CA2202,如何解决这种情况

CA2202, how to solve this case(CA2202,如何解决这种情况)
本文介绍了CA2202,如何解决这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何从以下代码中删除所有 CA2202 警告?

public static byte[] Encrypt(string data, byte[] key, byte[] iv){使用(MemoryStream memoryStream = new MemoryStream()){使用 (DESCryptoServiceProvider 密码 = 新的 DESCryptoServiceProvider()){使用 (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptograph.CreateEncryptor(key, iv), CryptoStreamMode.Write)){使用(StreamWriter streamWriter = new StreamWriter(cryptoStream)){streamWriter.Write(数据);}}}返回 memoryStream.ToArray();}}

<块引用>

警告 7 CA2202:Microsoft.Usage:对象cryptoStream"可以在方法CryptoServices.Encrypt(string, byte[], byte[])"中多次处理.为避免生成 System.ObjectDisposedException,您不应在一个对象上多次调用 Dispose.: Lines: 34

警告 8 CA2202:Microsoft.Usage:对象memoryStream"可以在方法CryptoServices.Encrypt(string, byte[], byte[])"中多次处理.为避免生成 System.ObjectDisposedException,您不应在一个对象上多次调用 Dispose.: Lines: 34, 37

您需要 Visual Studio 代码分析才能看到这些警告(这些不是 c# 编译器警告).

解决方案

这个编译没有警告:

 public static byte[] Encrypt(string data, byte[] key, byte[] iv){内存流内存流=空;DESCryptoServiceProvider 密码 = null;加密流加密流 = null;StreamWriter streamWriter = null;尝试{memoryStream = new MemoryStream();密码 = 新的 DESCryptoServiceProvider();cryptoStream = new CryptoStream(memoryStream, cryptograph.CreateEncryptor(key, iv), CryptoStreamMode.Write);var 结果 = 内存流;内存流=空;streamWriter = new StreamWriter(cryptoStream);加密流=空;streamWriter.Write(数据);返回结果.ToArray();}最后{如果(内存流!= null)memoryStream.Dispose();如果(密码!= null)cryptograph.Dispose();如果(加密流!= null)cryptoStream.Dispose();if (streamWriter != null)streamWriter.Dispose();}}

编辑回应评论:我刚刚再次验证此代码不会生成警告,而原始代码会生成.在原始代码中,CryptoStream.Dispose()MemoryStream().Dispose() 实际上被调用了两次(这可能是也可能不是问题).

修改后的代码的工作方式如下:只要将处置的责任转移到另一个对象,就会将引用设置为 null.例如.memoryStream 在调用 CryptoStream 构造函数成功后设置为 null.cryptoStream 设置为 null,在调用 StreamWriter 构造函数成功后.如果没有发生异常,streamWriter 会被释放到 finally 块中,然后依次释放 CryptoStreamMemoryStream.

Can anybody tell me how to remove all CA2202 warnings from the following code?

public static byte[] Encrypt(string data, byte[] key, byte[] iv)
{
    using(MemoryStream memoryStream = new MemoryStream())
    {
        using (DESCryptoServiceProvider cryptograph = new DESCryptoServiceProvider())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptograph.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                using(StreamWriter streamWriter = new StreamWriter(cryptoStream))
                {
                    streamWriter.Write(data);
                }
            }
        }
        return memoryStream.ToArray();
    }
}

Warning 7 CA2202 : Microsoft.Usage : Object 'cryptoStream' can be disposed more than once in method 'CryptoServices.Encrypt(string, byte[], byte[])'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 34

Warning 8 CA2202 : Microsoft.Usage : Object 'memoryStream' can be disposed more than once in method 'CryptoServices.Encrypt(string, byte[], byte[])'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 34, 37

You need Visual Studio Code Analysis to see these warnings (these are not c# compiler warnings).

解决方案

This compiles without warning:

    public static byte[] Encrypt(string data, byte[] key, byte[] iv)
    {
        MemoryStream memoryStream = null;
        DESCryptoServiceProvider cryptograph = null;
        CryptoStream cryptoStream = null;
        StreamWriter streamWriter = null;
        try
        {
            memoryStream = new MemoryStream();
            cryptograph = new DESCryptoServiceProvider();
            cryptoStream = new CryptoStream(memoryStream, cryptograph.CreateEncryptor(key, iv), CryptoStreamMode.Write);
            var result = memoryStream;              
            memoryStream = null;
            streamWriter = new StreamWriter(cryptoStream);
            cryptoStream = null;
            streamWriter.Write(data);
            return result.ToArray();
        }
        finally
        {
            if (memoryStream != null)
                memoryStream.Dispose();
            if (cryptograph != null)
                cryptograph.Dispose();
            if (cryptoStream != null)
                cryptoStream.Dispose();
            if (streamWriter != null)
                streamWriter.Dispose();
        }
    }

Edit in response to the comments: I just verified again that this code does not generate the warning, while the original one does. In the original code, CryptoStream.Dispose() and MemoryStream().Dispose() are actually called twice (which may or may not be a problem).

The modified code works as follows: references are set to null, as soon as responsibilty for disposing is transferred to another object. E.g. memoryStream is set to null after the call to CryptoStream constructor succeeded. cryptoStream is set to null, after the call to StreamWriter constructor succeeded. If no exception occurs, streamWriter is disposed in the finally block and will in turn dispose CryptoStream and MemoryStream.

这篇关于CA2202,如何解决这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)