问题描述
谁能告诉我如何从以下代码中删除所有 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
块中,然后依次释放 CryptoStream
和 MemoryStream
.
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,如何解决这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!