C# 中的预处理器无法正常工作
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于调试配置,您的项目设置应如下所示
对于发布,它们应如下所示
您能否验证情况是否如此,并告知我们是否如此?
如果没有,每种配置都有什么?
For Debug Configuration, your project settings should look like
For Release they should look like this
Can you verify that this is the case, and let us know if it is?
If not, what is there for each configuration?
使用所有默认设置创建一个新项目,并检查是否可以按预期工作。如果是这样,您的问题项目一定以某种方式“损坏”,可能是通过在发布配置中定义 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.
这取决于您如何创建配置。例如,如果您创建配置并使用调试或发布作为模板,则调试或发布将被复制到定义的约束元素。它不会将定义的约束元素(在项目文件中)更改为新的配置名称。
打开项目文件并查找配置部分。确保平台(以下示例为“PROD”)在 DefineConstants 元素中具有条目。如果确实如此,预编译器指令将无法按代码中的预期工作。
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.
为什么要把DEBUG放在括号里?
Why are you putting DEBUG between parentheses?
我猜想在您的项目属性中,在“构建”下您已选中
定义 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 forRelease
is that theDEBUG
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.