C# - 有选择地抑制自定义过时警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/968293/
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
C# - Selectively suppress custom Obsolete warnings
提问by Alex
I'm using the Obsolete
attribute (as just suggested by fellow programmers) to show a warning if a certain method is used.
Obsolete
如果使用某种方法,我正在使用该属性(正如其他程序员所建议的那样)来显示警告。
Is there a way to suppress the warning similar to CodeAnalysis' SuppressMessage
at points where the use is justified?
有没有办法SuppressMessage
在使用合理的地方抑制类似于 CodeAnalysis 的警告?
Thank you!
谢谢!
EDIT
编辑
This is for [Obsolete("Some message")]
as I need to include some details about the warning. However, #pragma warning disable 612
doesn't work anymore once I add the message to the naked [Obsolete]
attribute...
这是[Obsolete("Some message")]
因为我需要包含有关警告的一些详细信息。但是,#pragma warning disable 612
一旦我将消息添加到裸[Obsolete]
属性就不再起作用......
EDIT 2
编辑 2
Found the right warning number - It is 618if you have a message following the obsolete attribute.
找到正确的警告编号 -如果您在 obsolete 属性后面有一条消息,则为618。
So to do what I want to do:
所以做我想做的事:
#pragma warning disable 618
#pragma warning disable 618
and then after the call
然后在通话后
#pragma warning restore 618
#pragma warning restore 618
Thanks to Jared Par and Jon Skeet for pointing me in the right direction!
感谢 Jared Par 和 Jon Skeet 为我指明了正确的方向!
采纳答案by Jon Skeet
Use #pragma warning disable
:
使用#pragma warning disable
:
using System;
class Test
{
[Obsolete("Message")]
static void Foo(string x)
{
}
static void Main(string[] args)
{
#pragma warning disable 0618
// This one is okay
Foo("Good");
#pragma warning restore 0618
// This call is bad
Foo("Bad");
}
}
Restore the warning afterwards so that you won't miss "bad" calls.
之后恢复警告,这样您就不会错过“坏”电话。
回答by JaredPar
You're looking for the #pragma
warning disable directive
您正在寻找#pragma
警告禁用指令
Essentially you add the following command above the call site in the .cs file.
本质上,您在 .cs 文件中的调用站点上方添加以下命令。
#pragma warning disable 612
SomeMethodCall
612 is the error message ID for calling obsolete methods
612 是调用过时方法的错误消息 ID
回答by Jord?o
The intentis to disable the warning for obsolete usage, regardless of whether the construct is marked with [Obsolete]
or [Obsolete("Message")]
. So use both CS0612andCS0618:
该意图是禁用过时警告使用,无论是否结构标有[Obsolete]
或[Obsolete("Message")]
。所以同时使用CS0612和CS0618:
#pragma warning disable 612, 618
...
#pragma warning restore 612, 618
回答by Aaron Thoma
Here's how to get the warning/error numberin the first place:
以下是首先获取警告/错误编号的方法:
- Rebuildyour project.
- Go to the Outputwindow.
- Look up the line of the warning/error you want to suppress.
For example:C:\Users\Username\Documents\Visual Studio 2010\Projects\Projectname\Classname.cs(203,7): warning CS
0162
: Unreachable code detected
- Copy the number part after"
CS
". - Then proceed as Jon Skeet says.
- 重建你的项目。
- 转到输出窗口。
- 查找要抑制的警告/错误行。
例如:C:\Users\Username\Documents\Visual Studio 2010\Projects\Projectname\Classname.cs(203,7): warning CS
0162
: Unreachable code detected
- 复制“ ”后的数字部分
CS
。 - 然后按照 Jon Skeet所说的进行。
(Better alwaysproceed as Jon Skeetsays…)
(最好总是按照Jon Skeet所说的进行……)