我需要为Microsoft的一个类使用SecureString
,并在互联网上找到了以下代码:
public static class SecureStringExt
{
public static SecureString ConvertToSecureString(this string password)
{
if (password == null)
throw new ArgumentNullException("password");
unsafe //Red highlighted line
{
fixed (char* passwordChars = password)
{
var securePassword = new SecureString(passwordChars, password.Length);
securePassword.MakeReadOnly();
return securePassword;
}
}
}
}
唯一的问题是,unsafe
关键词一直报错,显示"Cannot use unsafe construct in safe context
"。
不幸的是,我找不到为什么会发生这种情况...
注意: 上述代码在LINQPad中运行正常,但在带有resharper的VS2013中无法运行。