我正在测试将 .NET 6.0 应用程序迁移到 .NET 8.0,下面的代码现在会抛出 CA1863“使用‘CompositeFormat’”。但我不知道怎么办。
private const string DUMP_DIALOGS_TITLE_FORMAT_STRING = "{0} Diagnostic Report";
public static string GetTitle(IProcess currentProcess)
{
return string.Format(CultureInfo.InvariantCulture, DUMP_DIALOGS_TITLE_FORMAT_STRING, currentProcess.ProcessName);
}
here给出的示例无效,因为
StaticField
变量未初始化,我不知道它应该是什么。
如果我将代码更改为
private static readonly CompositeFormat Format = CompositeFormat.Parse("{0} Diagnostic Report");
public static string GetTitle(IProcess currentProcess)
{
return string.Format(CultureInfo.InvariantCulture, Format, currentProcess.ProcessName);
}
Visual Studio 显示一条警告,指出我没有为 {0} 参数提供参数,即使一切看起来都按预期工作(我没有收到新代码的运行时错误)。难道只是 VS 代码分析还不知道
CompositeFormat
还是我遗漏了什么?
你的实现很好:
private static readonly CompositeFormat Format = CompositeFormat.Parse("{0} Diagnostic Report");
public static string GetTitle(IProcess currentProcess)
{
return string.Format(CultureInfo.InvariantCulture, Format, currentProcess.ProcessName);
}
该警告来自 ReSharper,目前仅支持 .NET 8 功能的基本功能(请参阅 JetBrains Rider 中的 .NET SDK 支持文档。)。
安装 EAP 版本(// ReSharper disable once FormatStringProblem
private static readonly CompositeFormat Format = CompositeFormat.Parse("{0} Diagnostic Report");