如何抑制特殊方法的“指定了格式,但参数不是 iformattable”消息

本文关键字:参数 指定了格式 消息 iformattable 格式 何抑制 方法 | 更新日期: 2023-09-27 18:18:49

ReSharpers StringFormatMethodAttribute 是跟踪执行字符串格式化的方法的参数的好方法。

所以我以这种方式实现了一个方法:

[StringFormatMethod("format")]
public string Format(string format, params object[] args)
{
    // do formatting by custom formatters
}

这让我有能力做这样的事情

Format("Hello {0}!", world);      // => Hello world!
Format("({0:D2} / {1:D2}", 2, 3); // => (02/03)

或在 Visual Basic 中

Format("Hello {0}!", visualBasic) ' => Hello Visual Basic!

而 ReSharper 会警告我,如果我错过了一个论点或其类似的立场

Format("Hello {1}!", world);
            // ^--- Non-existing argument in format string
Format("Hello {1}!", world, value);
                    // ^--- Argument is not used in format string

该方法正在使用ICustomFormatter所以如果我直接这样做

string.Format(customFormatter, "The thread id is {0:D}", Thread.CurrentThread);

ReSharper没有抱怨。

但是,使用我的方法在内部使用自定义格式,使ReSharper抱怨并发出警告

Format("The thread id is {0:D}", Thread.CurrentThread);
                         // ^--- Formatting is specified,
                         //      but argument is not IFormattable

一般不想禁用此警告,只想禁用格式问题部分,如果可能的话,只禁用我自己的编写方法。所以我的问题是,有没有办法,也许通过另一个属性让 ReSharper 意识到,这种方法在内部使用自定义格式化程序?

如何抑制特殊方法的“指定了格式,但参数不是 iformattable”消息

正如你(从用户名猜测(从citizenmatt的JetBrains开发者论坛上的一篇文章中了解到的那样,

遗憾的是,您无法禁用此分析。我添加了一个功能 请求,您可以投票,跟踪并添加任何额外的详细信息: https://youtrack.jetbrains.com/issue/RSRP-450291