visual-studio 有没有办法抑制 C# 中类似于 Java 的 @SuppressWarnings 注释的警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1378634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to suppress warnings in C# similar to Java's @SuppressWarnings annotation?
提问by Nosrama
Is there a way to suppress warnings in C# similar to Java's @SuppressWarnings annotation?
有没有办法抑制 C# 中类似于 Java 的 @SuppressWarnings 注释的警告?
Failing that, is there another way to suppress warnings in Visual Studio?
否则,是否有另一种方法可以抑制 Visual Studio 中的警告?
回答by Tamás Szelei
Yes.
是的。
For disabling, use:
要禁用,请使用:
#pragma warning disable 0169, 0414, anyothernumber
Where the numbers are the identifiers of the warnings that you can read from compiler output.
其中数字是您可以从编译器输出中读取的警告的标识符。
To reenable the warningsafter a particular part of code (which is a good idea) use:
要在代码的特定部分(这是个好主意)后重新启用警告,请使用:
#pragma warning restore 0169, anythingelse
This way you can make the compiler output clean, and keep yourself safe because the warnings will only be suppressed for that particular part of code (where you made sure you don't need to see them).
通过这种方式,您可以使编译器输出干净,并保证自己的安全,因为警告只会针对代码的特定部分(您确保不需要查看它们)被抑制。
回答by Robban
Yes there is you can use the pragma warning annotation like this:
是的,您可以像这样使用 pragma 警告注释:
#pragma warning disable 414
//some code that generates a warning
#pragma warning restore 414
omitting the numbers disables and restores all warning codes...
省略数字会禁用并恢复所有警告代码...
回答by Razzie
There is. See the MSDNpage on how to suppress compiler warnings.
有。请参阅有关如何抑制编译器警告的MSDN页面。
From Visual Studio, go to your project properties, select the build tab, and enter the warning number in the Suppress Warnings field.
在 Visual Studio 中,转到您的项目属性,选择构建选项卡,然后在 Suppress Warnings 字段中输入警告编号。
From code, to disable specific warnings, you can use the #pragma directive:
从代码中,要禁用特定警告,您可以使用 #pragma 指令:
public class MyClass
{
#pragma warning disable 0168
// code
// optionally, restore warnings again
#pragma warning restore 0168
// more code
}
回答by Sam Harwell
I highly recommend using the following form
我强烈建议使用以下表格
#pragma warning disable 649 // Field 'field' is never assigned to, and will always have its default value 'value'
#pragma warning restore 649
The comment on the first line is taken from the first like of the MSDN documentation for Compiler Warning (level 4) CS0649. Since warnings are numbered in C#, this is your only reference to what's actually going on in the code when you see a warning disabled. Placing it at the end of the line is the only way to get the reason to show up in the search results window when you do a search in your whole solution for pragma warning.
第一行的注释取自Compiler Warning (level 4) CS0649的 MSDN 文档中的第一个。由于警告在 C# 中编号,因此当您看到警告被禁用时,这是对代码中实际发生的事情的唯一参考。当您在整个解决方案中搜索pragma warning时,将它放在行尾是获得显示在搜索结果窗口中的唯一方法。
You can identify the warning numbers by looking in the Outputwindow after building your project. Make sure it says Show output from: Build.
构建项目后,您可以通过查看“输出”窗口来识别警告编号。确保它显示 Show output from: Build。
回答by Marcin Deptu?a
You could check #pragma directives: http://msdn.microsoft.com/en-us/library/441722ys(VS.80).aspx.
您可以检查 #pragma 指令:http: //msdn.microsoft.com/en-us/library/441722ys(VS.80).aspx。
回答by HerpDerpington
Have a look at the SuppressMessageAttributein VisualStudio: http://msdn.microsoft.com/en-us/library/ms182068.aspx
看看SuppressMessageAttribute在 VisualStudio:http: //msdn.microsoft.com/en-us/library/ms182068.aspx
回答by TroySteven
You can use the SuppressMessage data annotation which will prevent the warning.
您可以使用 SuppressMessage 数据注释来防止警告。
It looks something like this:
它看起来像这样:
[SuppressMessage("Reason #Enter whatever you'd like", "ID, must match what intellsense is showing it looks something like this: IDE0001", Justification = "(optional, your own description")]
Here is a real world example:
这是一个真实世界的例子:
[SuppressMessage("IntelliSenseCorrection", "IDE0001", Justification = "Do Not Remove <T> Variable, It's Required For Dapper")]
回答by Will Marcouiller
I guess you could also try to review the project or solution properties and set your warning level to a lower level or so. Otherwise, the other responses are perhaps better.
我想您也可以尝试查看项目或解决方案属性并将警告级别设置为较低级别左右。否则,其他响应可能会更好。

