简体   繁体   English

#if preprocessor指令用于DEBUG以外的指令

[英]#if preprocessor directive for directives other than DEBUG

I know that I can use preprocessor directives to check for Debug/Release by doing this: 我知道我可以通过这样做来使用预处理器指令来检查Debug / Release:

#if DEBUG
    //debug mode
#elif
    //release mode
#endif

but what about checking for other configurations, like Test. 但是如何检查其他配置,比如Test。 In VB you can do this: 在VB中你可以这样做:

#If CONFIG = "Release" Then
    'Release mode
#ElseIf CONFIG = "Test" Then
    'Test mode
#ElseIf CONFIG = "Debug" Then
    'Debug mode
#End If

So, my question is in C#, how can I check for Test mode? 所以,我的问题是在C#中,我该如何检查测试模式? I have some code that I want to execute if I'm in Debug AND Test, but not in Release mode, so specifically, I need a way to check for not being in Release mode. 我有一些代码,如果我在调试和测试中,我想要执行,但不是在发布模式,所以具体来说,我需要一种方法来检查不在发布模式。 In VB I would do this: 在VB中我会这样做:

#If Not CONFIG = "Release" Then
    'Do something here for every configuration that is not Release
#End If

It's the same as for DEBUG, assuming that you've defined a build configuration that lists TEST in the "Conditional compilation symbols" text box (under project properties > Build tab; this is a space-delimited list). 它与DEBUG相同, 假设您已在“条件编译符号”文本框中定义了一个列出TEST的构建配置 (在项目属性>构建选项卡下;这是一个以空格分隔的列表)。

For code that you only want to run in the TEST build configuration: 对于您只想在TEST构建配置中运行的代码:

#if TEST
// ...
#endif

And for code you don't want to run in the TEST build configuration, you can either #else the above, or do this: 对于您不想在TEST构建配置中运行的代码,您可以#else以上,或者执行以下操作:

#if !TEST
// ...
#endif

There are a couple ways to handle your factorings. 有几种方法可以处理您的因子。 In my world, we used four primary techniques: 在我的世界里,我们使用了四种主要技术:

  1. compiler flags ( #if ) 编译器标志( #if
  2. partial classes 部分课程
  3. separate implementations 单独的实现
  4. Runtime decisions 运行时决定

So for example, we have build configurations for, C# with unmanaged code, C# with all managed code, C# for silverlight. 例如,我们为C#构建配置,使用非托管代码,C#使用所有托管代码,C#使用Silverlight。 In the C# unmanaged project we have a compile-time symbol UNMANAGED , for C# we have MANAGED and for the silverlight we have SILVERLIGHT . 在C#unmanaged项目中,我们有一个编译时符号UNMANAGED ,对于C#我们有MANAGED而对于silverlight,我们有SILVERLIGHT This lets me inject small tasks into the code and share the same files across all projects. 这使我可以将小任务注入代码并在所有项目中共享相同的文件。 No big deal. 没什么大不了。

For partial classes, we have separate .cs files for each project that have implementations of the fringe code. 对于部分类,我们为每个具有边缘代码实现的项目都有单独的.cs文件。 This gets used in the cases where we couldn't make this work by having an abstract class as the parent class with most of the implementation and then the fringe code in concrete classes for each target. 在我们无法通过将抽象类作为具有大部分实现的父类,然后在每个目标的具体类中使用边缘代码来实现这项工作的情况下使用它。 This works well enough. 这很好用。

For separate implementations, we acknowledge that there is little that can be shared between the code bases and we're better off with separate code. 对于单独的实现,我们承认代码库之间几乎没有共享,我们最好使用单独的代码。 This is not ideal, but so be it. 这不是理想的,但也是如此。

For runtime checks, it's exactly that. 对于运行时检查,就是这样。 Rather than check for DEBUG in a #if , you use a runtime check for a setting to make that choice. 而不是在#if检查DEBUG ,而是使用运行时检查来进行设置以进行该选择。 Unless you've got heinously huge debug scaffolding, this is not a bad choice as it also lets you do field debugging (but you may have delivery constraints that prevent it). 除非你有非常庞大的调试脚手架,否则这不是一个糟糕的选择,因为它还允许你进行现场调试(但是你可能有阻止它的传输限制)。

Personally, I try to avoid the compiler flags. 就个人而言,我试图避免编译器标志。 They make the code harder to read. 它们使代码更难阅读。 Honestly, though, there are times where they make sense. 但老实说,有时它们才有意义。 We've had classes that wouldn't compile in silverlight just because of the class declaration (I think it was ObservableCollection that wasn't available) and we had to inherit from something else. 由于类声明(我认为ObservableCollection不可用),我们已经有了不能在Silverlight中编译的类,我们不得不继承其他东西。 Everything else worked fine. 其他一切都很好。

Right click on the Project [Project name] name you want to use the custom precompiler directive. 右键单击要使用自定义预编译器指令的Project [项目名称]名称。

Go to the properties item and then to the build tab. 转到属性项,然后转到构建选项卡。

then you need to add your custom directive there in the textbox. 那么你需要在文本框中添加自定义指令。 Eg i have added 'Local' as my custom directive see image below 例如,我添加了'Local'作为我的自定义指令,见下图

在此输入图像描述

Now you can can use the new compiler directive as shown below in your (in C#) 现在您可以使用新的编译器指令,如下所示(在C#中)

  #if **Local**
    //TODO:Add your c# code here
  #endif

Simple answer is 简单的答案是

  • Go to Project->[Project name] Properties->Build 转到项目 - > [项目名称]属性 - >构建
  • Set checked [] Define DEBUG 设置选中[]定义DEBUG

Now you can play with DEBUG predecessor directive like 现在您可以使用DEBUG前身指令了

#if DEBUG
...
#else
...
#endif

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

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