Windows phone 7 代码分析显示不多次处理对象

Windows phone 7 代码分析显示不多次处理对象,windows-phone-7,code-analysis,Windows Phone 7,Code Analysis,我的密码是 { AesManaged aes = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000); aes = n

我的密码是

{
    AesManaged aes = null;
    MemoryStream memoryStream = null;
    CryptoStream cryptoStream = null;

    try
    {
        Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

        aes = new AesManaged();
        aes.Key = rfc2898.GetBytes(32);
        aes.IV = rfc2898.GetBytes(16);

        memoryStream = new MemoryStream();
        cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

        byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
        cryptoStream.Write(data, 0, data.Length);
        cryptoStream.FlushFinalBlock();

        return Convert.ToBase64String(memoryStream.ToArray());
    }
    finally
    {
        if (cryptoStream != null)
            cryptoStream.Close();

        if (memoryStream != null)
            memoryStream.Close();

        if (aes != null)
            aes.Clear();
    }
}
我刚试过代码分析,它给了我

CA2202
Do not dispose objects multiple times 
Object 'memoryStream' can be disposed more than once in method 'EncryptDecrypt.Encrypt(string, string, string)'. 
To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

但我的代码运行良好我创建了memoryStream和cryptoStream并在。。。但是我不明白为什么它会多次告诉我多个对象

IDisposable的指南规定,两次处理同一个对象不会对第二次产生任何影响

然而,并不是所有的实现都遵循这个准则,所以代码分析告诉您不要依赖它


您的特定对象在这方面是安全的,因此您没有实际问题。

您应该使用
using
语句。(尽管它不能解决这个问题)