c# - 无法使用值 C# 初始化按引用变量
问题描述
出现此错误无法使用值初始化引用变量我对 C# 相当陌生,只是试图修复一个废弃的开源项目。
这是代码:
private bool CertificateHandler(bool valueExist)
{
if (!CertMaker.rootCertExists() && !CertMaker.createRootCert() || (CertMaker.rootCertIsTrusted() ? 1 : (CertMaker.trustRootCert() ? 1 : 0)) == 0)
return false;
// ISSUE: explicit reference operation
ref string local1 = @this.fiddlerCertInfos._fiddlerCert;
if (local1 == null)
local1 = FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.bc.cert", (string) null);
// ISSUE: explicit reference operation
ref string local2 = @this._fiddlerCertInfos._privateKey;
if (local2 == null)
local2 = FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.bc.key", (string) null);
if (!valueExist)
this.appRegistry.UpdateRegistry(new List<RegistryInfo>()
{
new RegistryInfo()
{
Name = "FiddlerCert",
Value = (object) this._fiddlerCertInfos._fiddlerCert,
RegistryValueKind = RegistryValueKind.String
},
new RegistryInfo()
{
Name = "PrivateKey",
Value = (object) this._fiddlerCertInfos._privateKey,
RegistryValueKind = RegistryValueKind.String
}
});
return true;
}
错误行:
ref string local1 = @this.fiddlerCertInfos._fiddlerCert;
ref string local2 = @this._fiddlerCertInfos._privateKey;
解决方案
读这个:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#ref-locals
您不能为局部引用变量分配非引用值。因此,要么您需要删除局部变量的 ref 关键字,要么在分配的值前面添加“ref”。
推荐阅读
- javascript - 如何连续运行窗口滚动事件?
- python - 如何在colab中实时标记网络摄像头上的关键点?
- python - list[-1] 不针对列表的最后一项(回文中有两个连续的零)
- angular - Angular 应用程序可以通过 eureka 服务器调用 Springboot 微服务中的 Api 吗?
- python - 漂亮的汤代码返回“AttributeError”
- java - 如何在 POST Retrofit 中发布 JSONObject / JSONArray?
- batch-file - 使批处理 FOR /F 忽略注册表自动运行
- php - 当我尝试分离数据时调用成员函数(laravel)
- angular - 反应式表单如何可扩展和可重用?
- java - 当用户离开播放视频的页面时,从适配器类暂停 ExoPlayer