如何禁止格式化,但参数不是iformattable"特殊方法的消息

时间:2015-10-22 20:28:47

标签: c# vb.net resharper

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知道,这个方法是在内部使用自定义格式化程序?

1 个答案:

答案 0 :(得分:1)

正如您(通过用户名猜测)from a post on the JetBrains developer forum了解citizenmatt

  

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