谁能告诉我如何从以下代码中删除所有CA2202警告?
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();
}
}
警告7 CA2202:Microsoft。用法:对象“ cryptoStream”可以在方法“ CryptoServices.Encrypt(string,byte [],byte [])”中处置多次。为了避免生成System.ObjectDisposedException,不应在一个对象上调用Dispose多次。:行:34
警告8 CA2202:Microsoft。用法:对象'memoryStream'可以在方法'CryptoServices.Encrypt(string,byte [],byte [])'中多次处置。为了避免生成System.ObjectDisposedException,不应在一个对象上调用Dispose多次。:行:34,37
您需要Visual Studio代码分析才能查看这些警告(这些不是c#编译器警告)。
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification="BrainSlugs83 said so.")]
”-确保using System.Diagnostics.CodeAnalysis;
在usings块中有一个“ ”语句。