简体   繁体   English

#if RELEASE 会像 C# 中的 #if DEBUG 那样工作吗?

[英]Will #if RELEASE work like #if DEBUG does in C#?

In all the examples I've seen of the #if compiler directive, they use "DEBUG".在我看到的所有关于 #if 编译器指令的示例中,它们都使用“DEBUG”。 Can I use "RELEASE" in the same way to exclude code that I don't want to run when compiled in debug mode?我可以以相同的方式使用“RELEASE”来排除在调试模式下编译时不想运行的代码吗? 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.我想用这个块包围的代码发送了一堆电子邮件,我不想在测试时不小心将它们发送出去。

RELEASE is not defined, but you can use RELEASE未定义,但您可以使用

#if (!DEBUG)
  ...
#endif

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不会在任何配置下生成任何代码。

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.虽然在调试配置中有一个DEBUG定义的常量(由 Visual Studio 自动定义),而没有为发布模式定义这样的常量。 Check your project settings under build.在构建下检查您的项目设置。

Selecting [Define DEBUG constant] under Project -> Build is 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 symbols textbox enter: RELEASE条件编译符号文本框中输入:RELEASE

On my VS install (VS 2008) #if RELEASE does not work.在我的 VS 安装 (VS 2008) 上, #if RELEASE不起作用。 However you could just use #if !DEBUG但是你可以只使用#if !DEBUG

Example:例子:

#if !DEBUG
SendTediousEmail()
#endif

I've never seen that before...but I have seen:我以前从未见过……但我见过:

#if (DEBUG == FALSE)

and

#if (!DEBUG)

That work for ya?那对你有用吗?

为此,您可以使用#if(!DEBUG)

"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.例如,您可以在同一个解决方案中拥有“DEBUG”、“DEMO”和“RELEASE”配置。 That prevents the need for duplicate programming with two different solutions.这可以防止需要使用两种不同的解决方案进行重复编程。

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)在定义 RELEASE 条件编译符号时与#if DEBUG工作方式相同。

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 symbols textbox enter: RELEASE以下摘自“Pop Catalin”帖子:如果要为发布配置定义 RELEASE 常量,请转到:* 项目属性 -> 构建* 选择发布模式 * 在条件编译符号文本框中输入:RELEASE

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.您可以出于特定原因使用此构造,例如如果需要,可以使用不同的客户端,甚至可以使用整个 Web 方法。 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.过去我们也使用过它,其中一些命令会导致特定硬件出现问题,因此我们在部署到硬件 X 时为应用程序配置了一个配置。

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".您还可以“在 C# 代码文件的顶部”定义它们。 Like:喜欢:

#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

Whilst M4N's answer ( #if (!DEBUG) ) makes most sense, another option could be to use the preprocessor to amend other flag's values;虽然M4N 的答案#if (!DEBUG) )最有意义,但另一种选择可能是使用预处理器来修改其他标志的值; eg例如

bool isRelease = true;
#if DEBUG
    isRelease = false;
#endif

Or better, rather than referring to whether we're in release or debug mode, use flags that define the expected behavior and set them based on the mode:或者更好,而不是指我们是处于发布模式还是调试模式,使用定义预期行为的标志并根据模式设置它们:

bool sendEmails = true;
#if DEBUG
    sendEmails = false;
#endif

This is different to using preprocessor flags, in that the flags are still there in production, so you incur the overhead of if (sendEmails) {/* send mails */} each time that code's called, rather than the code existing in release but not existing in debug, but this can be advantageous;这与使用预处理器标志不同,因为这些标志仍在生产中,因此每次调用该代码时都会产生if (sendEmails) {/* send mails */}的开销,而不是 release 中存在的代码,但是不存在于调试中,但这可能是有利的; eg in your tests you may want to call your SendEmails() method but on a mock, whilst running in debug to get additional output.例如,在您的测试中,您可能想要调用SendEmails()方法,但在模拟上,同时在调试中运行以获得额外的输出。

Another option:另外一个选项:

#If CONFIG = "Release" Then

....

#End If

why not just为什么不只是

#if RELEASE
#undef DEBUG
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM