I have code which I would like to ena开发者_StackOverflow中文版ble for compilation when I build project using Release configuration and disable while debugging. How to do what?
Use a preprocessor directive.
#IF ! DEBUG
//Your code here
#ENDIF
Though, if your code is full of these, you may want to consider other options, such as
Another alternative is to use the ConditionalAttribute
on a method so it will only be used if a specific symbol has been defined:
[Conditional("RELEASE")]
public void MyReleaseMethod()
{
}
Use a preprocessor directive. Surround the code with:
#if !DEBUG
// Release-only code goes here...
#endif
In the standard debug and release configurations in Visual Studio, the DEBUG
symbol is defined when compiling in debug and not in release, so code in between the two directives above will only be compiled in release mode.
If you need to do one thing in debug and another thing in release, you can do this:
#if DEBUG
// Debug-only code goes here...
#else
// Release-only code goes here...
#endif
See the C# preprocessor documentation for more details.
精彩评论