'System.Security.Cryptography.Rfc2898DeriveBytes': using 语句中使用的类型必须可以隐式转换为 System.IDisposable

'System.Security.Cryptography.Rfc2898DeriveBytes': type used in a using statement must be implicitly convertible to System.IDisposable

我的代码在 .Net Framework 4.5 中运行,但是当我尝试在 .Net Framework 2.0 中编写相同的代码时,出现编译错误

'System.Security.Cryptography.Rfc2898DeriveBytes': type used in a using statement must be implicitly convertible to System.IDisposable

如何解决?

代码

[ComVisible(true)]
    public string HashPassword(string password)
    {

        byte[] salt;
        byte[] subkey;
        using (var deriveBytes = new Rfc2898DeriveBytes(password, SaltSize, PBKDF2IterCount))
        {
            salt = deriveBytes.Salt;
            subkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);
        }

        var outputBytes = new byte[1 + SaltSize + PBKDF2SubkeyLength];
        //some more code goes here
        return Convert.ToBase64String(outputBytes);
    }

André's comment, it would appear that the Dispose method wasn't introduced until .NET 4: DeriveBytes.Dispose中汲取灵感 (注:"Other Versions")

您可以看看 Dispose 方法在当前框架中是如何实现的,并考虑将其应用到您的项目中:Source Browser

如果删除 using 怎么样:

 Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, SaltSize, 
 PBKDF2IterCount);
 salt = deriveBytes.Salt;
 subkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);