我需要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 中运行,但在 VS2013 中没有(使用 resharper)。