开发者

Enable code on Release compilation

开发者 https://www.devze.com 2023-03-24 18:10 出处:网络
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 dire

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.

0

精彩评论

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