开发者

Using Preprocessor Directives in Visual Studio 2010 with C#

I have a C# application tha开发者_运维问答t I am building with Visual Studio 2010. To help me with some of my routine tasks in the app, I wanted to set some values if I compiled the app in debug mode. Naturally, I though preprocessor directives would be a good idea. My problem is, I don't quite understand how to use them. At this time, I have a block of code that looks like this:

#define DEBUG

... // Other code in my app

#if DEBUG
  myVariable = debugValue;
#endif

My problem is, when I compile my app in release mode, myVariable still gets set to debugValue. It's like I'm not properly defining my preprocessor variable or I'm not configuring my compiler correctly. Can anybody explain to me what I need to do so that myVariable is only set to debugValue when I compile the app in debug mode?

Thank you!


If you are using #define DEBUG to specify the debug symbol, switching to release mode will still provide the symbol, since you are explicitly defining it.

Try removing the #define DEBUG line in your code files. By default VS defines DEBUG and TRACE in debug mode and TRACE in release mode, so its not necessary to explicitly define them.


If you aren't aware of it, you might want to check out the "Conditional" attribute. This lets you decorate a method rather than inlining preprocessor directives:

class SomeClass 
{
   public void ProductionOperation()
   {
      //Doin' production stuff
       Log(someProductionVariable);
   }

   [Conditional("DEBUG")]
   public static void Log(string message) 
   {
       //Write to a file
   }
}

If you're compiling in debug mode, the log method will write to a file. If you're compiling in release mode, the conditional method becomes a no-op. Only thing to bear in mind here is that the conditional code will make it into your assembly, unlike if you preempt it with the preprocessor - this is a runtime distinction. However, as long as you don't mind that, I've found this keeps the code cleaner.

(If you're going to do this, you don't want to be #defining or #undefining the DEBUG variable anywhere in your code).


The DEBUG constant is actually defined in your project properties. Go to Project Properties->Build tab->Define DEBUG constant.

By explicitly declaring that constant, you are overriding the one declared by VS.


Just to add little to Kyle and Steve's answers..

If you open your .csproj file with a text editor you can see how the the configuration's Debug and Release define the Symbols.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    ...
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    ...
    <DefineConstants>TRACE</DefineConstants>
    ...
  </PropertyGroup>

The active config is set with

<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>

but as Kyle already mentioned your code essentially adds it to all configurations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜