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

发布于 2024-10-18 16:53:51 字数 290 浏览 2 评论 0原文

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

我有一些这样的代码。如果我的应用程序在调试模式下运行,则应执行 #if(DEBUG) 部分,如果在发布模式下运行,则应执行 #else 部分。但是,无论它在哪种模式下运行,它都只执行 #if(DEBUG) 部分。

我在 VS2010 中使用 WPF 应用程序

有人可以帮助我吗?

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

I have some code like this. If my application is running in Debug mode it should execute the #if(DEBUG) part, if it is running in Release mode it should execute the #else part. However, it is only executing the #if(DEBUG) part no matter which mode it is running in.

Am using WPF application with VS2010

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦行七里 2024-10-25 16:53:51

对于调试配置,您的项目设置应如下所示

在此处输入图像描述

对于发布,它们应如下所示

在此处输入图像描述

您能否验证情况是否如此,并告知我们是否如此?
如果没有,每种配置都有什么?

For Debug Configuration, your project settings should look like

enter image description here

For Release they should look like this

enter image description here

Can you verify that this is the case, and let us know if it is?
If not, what is there for each configuration?

冰雪之触 2024-10-25 16:53:51

使用所有默认设置创建一个新项目,并检查是否可以按预期工作。如果是这样,您的问题项目一定以某种方式“损坏”,可能是通过在发布配置中定义 DEBUG 常量,或者为发布解决方案配置选择调试项目配置。

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.

命硬 2024-10-25 16:53:51

这取决于您如何创建配置。例如,如果您创建配置并使用调试或发布作为模板,则调试或发布将被复制到定义的约束元素。它不会将定义的约束元素(在项目文件中)更改为新的配置名称。

打开项目文件并查找配置部分。确保平台(以下示例为“PROD”)在 DefineConstants 元素中具有条目。如果确实如此,预编译器指令将无法按代码中的预期工作。

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

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. 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. 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>
童话里做英雄 2024-10-25 16:53:51

为什么要把DEBUG放在括号里?

#if DEBUG
    Code
#else
    Code
#endif

Why are you putting DEBUG between parentheses?

#if DEBUG
    Code
#else
    Code
#endif
夏末染殇 2024-10-25 16:53:51

我猜想在您的项目属性中,在“构建”下您已选中定义 DEBUG 常量

尝试将配置模式设置为 Release 并再次运行您的应用程序。 Release 的默认值是未定义 DEBUG 常量,当然,如果您没有篡改 if ;)

如果 Define DEBUG Constant检查,这意味着您有一个#define DEBUG潜伏在某处。

所以要做两件事。检查Release模式下选项中的常量,并检查是否有任何手动定义的常量。它应该是其中之一。

I would guess that in your project properties, under Build you have checked off Define DEBUG constant.

Try setting configuration mode to Release and run your application again. Default for Release is that the DEBUG constant is not defined, if you haven't tampered with if of course ;)

If Define DEBUG constant is not checked, that means you have a #define DEBUG lurking somewhere.

So two things to do. Check constant in options under Release mode, and check for any manually defined constant. It should be one of those.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文