任何人都可以告诉我如何从以下代码中删除所有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();
}
}
Run Code Online (Sandbox Code Playgroud)
警告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#编译器警告).