条件属性

aki*_*kif 3 c# debugging conditional conditional-compilation visual-studio-2008

一个快速的C#问题,我想知道在我的项目> Properties> Build中,有一个检查"Define DEBUG constant",所以如果我检查然后执行此操作,

[Conditional(DEBUG)]
public static void Foo() {
      Console.WriteLine("Executed Foo");
}
Run Code Online (Sandbox Code Playgroud)

看到它不是"DEBUG"它的DEBUG常量.这样可以吗?或者我是否必须在项目设置中的条件编译符号中添加"DEBUG"?还是#define呢?

Jam*_*mes 5

我很确定你需要这样做:

[Conditional("Debug")] or [Conditional("DEBUG")]
Run Code Online (Sandbox Code Playgroud)

或者您可以定义自己的常量,例如:

const string DEBUG = "DEBUG";
Run Code Online (Sandbox Code Playgroud)

然后使用它

[Conditional(DEBUG)]
Run Code Online (Sandbox Code Playgroud)

这必须附有#define DEBUG声明.请参阅MSDN上的条件C#.