简体   繁体   English

C#中的预处理器无法正常工作

[英]Preprocessor in C# is not working

#if(DEBUG)
    ......Code......
#else
    ......Code......
#endif

I have some code like this. 我有一些像这样的代码。 if my application is running in Debug mode it should execute #if(DEBUG) part, if it is running in Release mode it should execute #else part. 如果我的应用程序在调试模式下运行,它应该执行#if(DEBUG)部分,如果它在Release模式下运行,它应该执行#else部分。 but whatever the mode it is running it is executing #if(DEBUG) part only. 但无论它运行的模式是什么,它只执行#if(DEBUG)部分。

Am using WPF application with VS2010 我在VS2010上使用WPF应用程序

Can anyone help me? 谁能帮我?

For Debug Configuration, your project settings should look like 对于调试配置,您的项目设置应如下所示

在此输入图像描述

For Release they should look like this 对于Release,它们看起来应该是这样的

在此输入图像描述

Can you verify that this is the case, and let us know if it is? 你能否证实是这种情况,如果有,请告诉我们?
If not, what is there for each configuration? 如果没有,每种配置有什么用?

Create a new project using all the default settings and check that you can make that work as expected. 使用所有默认设置创建一个新项目,并检查您是否可以按预期方式工作。 If so, your problem project must be "corrupted" in some way, perhaps by defining the DEBUG constant in the release configuration, or by having the debug project configuration selected for the release solution configuration. 如果是这样,您的问题项目必须以某种方式“损坏”,可能是通过在发布配置中定义DEBUG常量,或者为发布解决方案配置选择调试项目配置。

It depends on how you create your configurations. 这取决于您如何创建配置。 For example if you create your configuration and use debug or release as a template DEBUG or RELEASE will be copied to the defined constraints element. 例如,如果您创建配置并使用debug或release作为模板,则DEBUG或RELEASE将被复制到已定义的constraints元素。 It will not change the defined constraints element (in the project file) to the new configuration name. 它不会将已定义的约束元素(在项目文件中)更改为新的配置名称。

Open up the project file and look for the configuration sections. 打开项目文件并查找配置部分。 Make sure the Platform, the below example it is "PROD" has an entry in the DefineConstants element. 确保平台,下面的示例是“PROD”在DefineConstants元素中有一个条目。 If it does the pre-compiler directives will don't work as expected in the code. 如果是这样,预编译器指令将无法在代码中按预期工作。

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'PROD|x86'">
  <DefineConstants>PROD;TRACE</DefineConstants>
    <OutputPath>bin\x86\PROD\</OutputPath>
  </PropertyGroup>

I would guess that in your project properties, under Build you have checked off Define DEBUG constant . 我想在你的项目属性中,在Build你已经选中了Define DEBUG constant

Try setting configuration mode to Release and run your application again. 尝试将配置模式设置为Release并再次运行应用程序。 Default for Release is that the DEBUG constant is not defined, if you haven't tampered with if of course ;) Release默认值是没有定义DEBUG常量,如果你当然没有被篡改;)

If Define DEBUG constant is not checked, that means you have a #define DEBUG lurking somewhere. 如果选中Define DEBUG constant则表示您在某处潜伏着#define DEBUG

So two things to do. 所以有两件事要做。 Check constant in options under Release mode, and check for any manually defined constant. 在Release模式下检查选项中的常量,并检查是否有任何手动定义的常量。 It should be one of those. 它应该是其中之一。

Why are you putting DEBUG between parentheses? 你为什么把DEBUG放在括号之间?

#if DEBUG
    Code
#else
    Code
#endif

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

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