【发布时间】:2018-01-16 23:28:16
【问题描述】:
我的代码在 .Net 框架 4.5 中工作,但是当我尝试在 .Net 框架 2.0 中编写相同的代码时,它给出了编译错误
'System.Security.Cryptography.Rfc2898DeriveBytes':在 a 中使用的类型 using 语句必须隐式转换为 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);
}
【问题讨论】:
-
该错误表明您只能初始化从 IDispossble 接口继承的类的实例,并且 Rfc2898DeriveBytes 类不继承 IDisposable 接口。这就是您看到此错误的原因
-
@ChetanRanpariya 这不正确。它继承自 DeriveBytes,后者实现了 IDisposable 接口。
-
查看文档,不清楚IDisposable接口是否由.NET Framework 2.0中的抽象DeriveBytes类实现...
-
@Samra 检查您的 derivedBytes 变量是否有权访问 Dispose 方法.. Intellisense 是否批准 derivedBytes.Dispose() ?
-
DeriveBytes在 .net 2.0 中没有实现 IDisposable。 msdn.microsoft.com/en-us/library/…