3

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

[Conditional(DEBUG)]
public static void Foo() {
      Console.WriteLine("Executed Foo");
}

看到它不是“DEBUG”,它是DEBUG常量。那么这会好吗?还是我必须在项目设置的条件编译符号中添加“调试” ?还是#define

4

2 回答 2

5

我很确定你需要这样做:

[Conditional("Debug")] or [Conditional("DEBUG")]

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

const string DEBUG = "DEBUG";

然后用那个

[Conditional(DEBUG)]

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

于 2010-01-08T12:21:21.947 回答
3

您需要为此添加双引号:

[Conditional("DEBUG")] // <- Works the DEBUG define
public static void Foo() {
    Console.WriteLine("Executed Foo");
}
于 2010-01-08T12:22:20.327 回答