#if RELEASE 会像 C# 中的 #if DEBUG 那样工作吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/507704/
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
Will #if RELEASE work like #if DEBUG does in C#?
提问by Brian Sullivan
In all the examples I've seen of the #if compiler directive, they use "DEBUG". Can I use "RELEASE" in the same way to exclude code that I don't want to run when compiled in debug mode? The code I want to surround with this block sends out a bunch of emails, and I don't want to accidentally send those out when testing.
在我看到的所有关于 #if 编译器指令的示例中,它们都使用“DEBUG”。我可以以相同的方式使用“RELEASE”来排除在调试模式下编译时不想运行的代码吗?我想用这个块包围的代码发送了一堆电子邮件,我不想在测试时不小心将它们发送出去。
采纳答案by Lasse V. Karlsen
No, it won't, unless you do some work.
不,它不会,除非你做一些工作。
The important part here is what DEBUG really is, and it's a kind of constant defined that the compiler can check against.
这里的重要部分是 DEBUG 的真正含义,它是一种定义为编译器可以检查的常量。
If you check the project properties, under the Build tab, you'll find three things:
如果您检查项目属性,在 Build 选项卡下,您会发现三件事:
- A text box labelled "Conditional compilation symbols"
- A check box labelled "Define DEBUG constant"
- A check box labelled "Define TRACE constant"
- 标有“条件编译符号”的文本框
- 标有“定义调试常量”的复选框
- 标有“定义跟踪常量”的复选框
There is no such checkbox, nor constant/symbol pre-defined that has the name RELEASE.
没有这样的复选框,也没有预定义名为 RELEASE 的常量/符号。
However, you can easily add that name to the text box labelled Conditional compilation symbols, but make sure you set the project configuration to Release-mode before doing so, as these settings are per configuration.
但是,您可以轻松地将该名称添加到标记为条件编译符号的文本框中,但请确保在执行此操作之前将项目配置设置为发布模式,因为这些设置是针对每个配置的。
So basically, unless you add that to the text box, #if RELEASE
won't produce any code under any configuration.
所以基本上,除非您将其添加到文本框中,#if RELEASE
否则不会在任何配置下生成任何代码。
回答by M4N
RELEASE
is not defined, but you can use
RELEASE
未定义,但您可以使用
#if (!DEBUG)
...
#endif
回答by JaredPar
On my VS install (VS 2008) #if RELEASE
does not work. However you could just use #if !DEBUG
在我的 VS 安装(VS 2008)#if RELEASE
上不起作用。但是你可以使用#if !DEBUG
Example:
例子:
#if !DEBUG
SendTediousEmail()
#endif
回答by Pop Catalin
Nope.
不。
While in debug configuration there is a DEBUG
defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. Check your project settings under build.
虽然在调试配置中有一个DEBUG
定义的常量(由 Visual Studio 自动定义),而没有为发布模式定义这样的常量。在构建下检查您的项目设置。
Selecting [Define DEBUG constant] under Project -> Buildis like including #define DEBUG at the beginning of every file.
在Project -> Build下选择[Define DEBUG constant]就像在每个文件的开头包含#define DEBUG。
If you want to define a RELEASE constant for the release configuration go to:
如果要为发布配置定义 RELEASE 常量,请转到:
- Project Properties -> Build
- Select Release Mode
- in the Conditional compilation symbolstextbox enter: RELEASE
- 项目属性 -> 构建
- 选择释放模式
- 在条件编译符号文本框中输入:RELEASE
回答by Matt Davison
why not just
为什么不只是
#if RELEASE
#undef DEBUG
#endif
回答by Pete H.
I've never seen that before...but I have seen:
我以前从未见过……但我见过:
#if (DEBUG == FALSE)
and
和
#if (!DEBUG)
That work for ya?
那对你有用吗?
回答by Pete H.
"Pop Catalin" got it right. Controlling the definition based on the type of build provides a great deal of flexibility. For example, you can have a "DEBUG", "DEMO", and "RELEASE" configuration all in the same solution. That prevents the need for duplicate programming with two different solutions.
“流行卡塔林”说得对。根据构建类型控制定义提供了很大的灵活性。例如,您可以在同一个解决方案中拥有“DEBUG”、“DEMO”和“RELEASE”配置。这可以防止需要使用两种不同的解决方案进行重复编程。
So yes #if RELEASE
or #if (RELEASE)
works the same as #if DEBUG
when the RELEASE Conditional compilation symbol is defined.
所以是#if RELEASE
或#if (RELEASE)
与#if DEBUG
定义 RELEASE 条件编译符号时的工作方式相同。
The following is taken from "Pop Catalin" post: If you want to define a RELEASE constant for the release configuration go to: * Project Properties -> Build* Select Release Mode * in the Conditional compilation symbolstextbox enter: RELEASE
以下摘自“Pop Catalin”帖子:如果要为发布配置定义 RELEASE 常量,请转到:* 项目属性 -> 构建* 选择发布模式 * 在条件编译符号文本框中输入:RELEASE
回答by Fetchez la vache
I know this is an old question, but it might be worth mentioning that you can create your own configurations outside of DEBUG and RELEASE, such as TEST or UAT.
我知道这是一个老问题,但值得一提的是,您可以在 DEBUG 和 RELEASE 之外创建自己的配置,例如 TEST 或 UAT。
If then on the Build tab of the project properties page you then set the "Conditional compilation symbols" to TEST (for instance) you can then use a construct such as
如果然后在项目属性页面的 Build 选项卡上将“条件编译符号”设置为 TEST(例如),则可以使用诸如
#if (DEBUG || TEST )
//Code that will not be executed in RELEASE or UAT
#endif
You can use this construct for specific reason such as different clients if you have the need, or even entire Web Methods for instance. We have also used this in the past where some commands have caused issues on specific hardware, so we have a configuration for an app when deployed to hardware X.
您可以出于特定原因使用此构造,例如如果需要,可以使用不同的客户端,甚至可以使用整个 Web 方法。过去我们也使用过它,其中一些命令会导致特定硬件出现问题,因此我们在部署到硬件 X 时为应用程序进行了配置。
回答by Abdus Salam Azad
You can use #if(!DEBUG)
for this purposes.
您可以#if(!DEBUG)
用于此目的。
回答by King Coffee
You can create you own conditional compile-time symbols (any name you like). Go to the "project Build dialog", located in the project properties box, menu option: Project->[projectname] Properties...
您可以创建自己的条件编译时符号(任何您喜欢的名称)。转到位于项目属性框中的“项目构建对话框”,菜单选项:项目->[项目名称] 属性...
You can also define them "at the top of the C# code file". Like:
您还可以“在 C# 代码文件的顶部”定义它们。喜欢:
#define RELEASE
// or
#undef RELEASE
you can use the symbol in a #if statement:
您可以在 #if 语句中使用该符号:
#if RELEASE
// code ...
#elif …
// code ...
#endif
// or
#if !RELEASE
// code ...
#endif