gpt4 book ai didi

c# - 如何为特殊方法抑制 "formatting is specified but argument is not iformattable"消息

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:49 24 4
gpt4 key购买 nike

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

最佳答案

正如您(从用户名猜测)了解到的那样from a post on the JetBrains developer forum通过 citizenmatt ,

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

关于c# - 如何为特殊方法抑制 "formatting is specified but argument is not iformattable"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com