开发者

Preprocessor in C# is not working correctly

#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 exec开发者_运维技巧uting the #if(DEBUG) part no matter which mode it is running in.

Am using WPF application with VS2010

Can anyone help me?


For Debug Configuration, your project settings should look like

Preprocessor in C# is not working correctly

For Release they should look like this

Preprocessor in C# is not working correctly

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.


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>


Why are you putting DEBUG between parentheses?

#if DEBUG
    Code
#else
    Code
#endif


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜