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

时间:2018-01-16 23:28:16

标签: c# .net using

我在.Net framework 4.5中使用了代码,但是当我尝试在.Net framework 2.0中编写相同的代码时,它会出现编译错误

  

'System.Security.Cryptography.Rfc2898DeriveBytes':用于   using语句必须可以隐式转换为System.IDisposable

如何解决?

CODE

[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);
    }

2 个答案:

答案 0 :(得分:1)

André's评论中汲取灵感,似乎在.NET 4之前不会引入Dispose方法:DeriveBytes.Dispose (注意:"其他版本")

您可以查看当前框架中Dispose方法的实现方式,并考虑根据您的项目进行调整:Source Browser

答案 1 :(得分:0)

如果删除using之类的内容如何:

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