我使用了代码分析工具,它提供了以下警告.
严重级代码描述项目文件行抑制状态警告CA2202对象'stream'可以在方法'Cipher.Encryptor(string)'中多次处理.为避免生成System.ObjectDisposedException,不应在对象上多次调用Dispose:Lines:43 Service Layer ...\Cipher.cs 43 Active
它源于"权力之塔":
public static string Encryptor(string input)
{
var content = String.Empty;
var cipher = new RijndaelManaged();
var plain = Encoding.Unicode.GetBytes(input);
var key = new PasswordDeriveBytes(password, salt);
using (var encrypt = cipher.CreateEncryptor(key.GetBytes(32), key.GetBytes(16)))
using (var stream = new MemoryStream())
using (var cryptographic = new CryptoStream(stream, encrypt, CryptoStreamMode.Write))
{
cryptographic.Write(plain, 0, plain.Length);
cryptographic.FlushFinalBlock();
content = Convert.ToBase64String(stream.ToArray());
}
return content;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我利用MemoryStream,CryptoStream和ICryptoTransform.为什么Visual Studio的代码分析会将此标记为警告?这来自Visual Studio 2015中内置的代码分析.
=================
这是一个较短的repro:
using System.IO;
using System.Security.Cryptography;
namespace ClassLibrary1
{
public class Class1
{
void foo()
{
using (var memStream = new MemoryStream())
using (var xForm = new FromBase64Transform())
using (var cStream = new CryptoStream(memStream, xForm, CryptoStreamMode.Read))
;
}
}
}
Run Code Online (Sandbox Code Playgroud)
......这可以在VS2013 Up5中看到
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |