C# #if 预处理器指令,用于除 DEBUG 之外的指令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13990919/
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
#if preprocessor directive for directives other than DEBUG
提问by Johnie Karr
I know that I can use preprocessor directives to check for Debug/Release by doing this:
我知道我可以使用预处理器指令通过执行以下操作来检查调试/发布:
#if DEBUG
//debug mode
#elif
//release mode
#endif
but what about checking for other configurations, like 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? 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:
所以,我的问题是在 C# 中,如何检查测试模式?如果我处于调试和测试状态,但不是处于发布模式,我有一些我想执行的代码,所以具体来说,我需要一种方法来检查是否处于发布模式。在VB中我会这样做:
#If Not CONFIG = "Release" Then
'Do something here for every configuration that is not Release
#End If
采纳答案by Joe White
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
回答by plinth
There are a couple ways to handle your factorings. In my world, we used four primary techniques:
有几种方法可以处理您的因式分解。在我的世界中,我们使用了四种主要技术:
- compiler flags (
#if
) - partial classes
- separate implementations
- Runtime decisions
- 编译器标志 (
#if
) - 部分类
- 单独的实现
- 运行时决策
So for example, we have build configurations for, C# with unmanaged code, C# with all managed code, C# for 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
. This lets me inject small tasks into the code and share the same files across all projects. No big deal.
例如,我们为 C# 与非托管代码、C# 与所有托管代码以及 Silverlight 的 C# 构建配置。在 C# 非托管项目中,我们有一个编译时符号UNMANAGED
,对于 C# 我们有MANAGED
,对于 Silverlight 我们有SILVERLIGHT
。这让我可以将小任务注入代码并在所有项目中共享相同的文件。没什么大不了。
For partial classes, we have separate .cs files for each project that have implementations of the fringe code. 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.
对于部分类,我们为每个具有边缘代码实现的项目提供单独的 .cs 文件。这在我们无法通过将抽象类作为父类与大多数实现然后在每个目标的具体类中的边缘代码来完成这项工作的情况下使用。这工作得很好。
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. 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).
对于运行时检查,正是如此。不是DEBUG
在 a 中检查,而是#if
使用运行时检查设置来做出选择。除非您有非常庞大的调试支架,否则这不是一个糟糕的选择,因为它还可以让您进行现场调试(但您可能会遇到阻止它的交付限制)。
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. Everything else worked fine.
就个人而言,我尽量避免使用编译器标志。它们使代码更难阅读。不过,老实说,有时它们是有意义的。我们有一些类因为类声明而无法在 Silverlight 中编译(我认为是 ObservableCollection 不可用),我们不得不从其他东西继承。其他一切工作正常。
回答by user3477720
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
回答by Ram
Right click on the Project [Project name] name you want to use the custom precompiler directive.
右键单击要使用自定义预编译器指令的项目 [项目名称] 名称。
Go to the properties item and then to the build tab.
转到属性项,然后转到构建选项卡。
then you need to add your custom directive there in the textbox. E.g i have added 'Local' as my custom directive see image below
然后您需要在文本框中添加您的自定义指令。例如,我添加了“本地”作为我的自定义指令,请参见下图
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