简体   繁体   English

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

[英]How to suppress "formatting is specified but argument is not iformattable" message for special methods

ReSharpers StringFormatMethodAttribute is a great way to keep track of the arguments on methods which perform string formatting. ReSharpers StringFormatMethodAttribute是跟踪执行字符串格式的方法的参数的好方法。

So I implemented a method that way:所以我以这种方式实现了一个方法:

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

This gives me the ability to do things like that这让我有能力做这样的事情

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

or in Visual Basic或在 Visual Basic 中

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

while ReSharper warns me if I miss an argument or its position like so而 ReSharper 会警告我,如果我错过了一个论点或它的位置,就像这样

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

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

The method is using an ICustomFormatter so if I'd do this directly该方法使用ICustomFormatter所以如果我直接这样做

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

ReSharper does not complain. ReSharper 不会抱怨。

Using my method, which uses the custom formatting internally however, makes ReSharper complain with a warning使用我的方法,但它在内部使用自定义格式,使 ReSharper 抱怨警告

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

I don't want to disable this warnings in general, only the formatting issue part and if possible only for my own written methods.我一般不想禁用此警告,仅禁用格式问题部分,如果可能,仅禁用我自己编写的方法。 So my question is, is there a way, maybe by another attribute to make ReSharper aware, that this method is using a custom formatter internally?所以我的问题是,有没有办法,也许是通过另一个属性让 ReSharper 意识到这个方法在内部使用自定义格式化程序?

As you (guessing from the user name) learned from a post on the JetBrains developer forum by citizenmatt ,正如您(根据用户名猜测)Citizenmatt在 JetBrains 开发者论坛上的帖子中了解到的

Unfortunately, you can't disable this analysis.不幸的是,您无法禁用此分析。 I've added a feature request, which you can vote for, track and add any extra details: https://youtrack.jetbrains.com/issue/RSRP-450291我添加了一个功能请求,您可以投票支持、跟踪和添加任何额外的详细信息: https : //youtrack.jetbrains.com/issue/RSRP-450291

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM