C#当Debug语句在Release模式下运行时被编译出来 [英] C# Do Debug statements get compiled out when running in Release mode?
问题描述
看起来似乎是理想的(在可读性方面)使用说 Debug.WriteLine
写入输出而不是一吨的 #if DEBUG
语句。
It seems like it would be ideal (in terms of readability) to use say Debug.WriteLine
to write to output rather than a ton of #if DEBUG
statements.
当程序在发行模式下编译时,所有开销与 Debug。 WriteLine
如果它不存在,或者是仍然被调用的函数,但是内部没有内容呢?
When the program is compiled in release mode, does all the overhead with the Debug.WriteLine
go away as if it did not exist, or is the function still called, but nothing done internally?
如果是,是否有在自定义类中获取此功能的方式,即静态调用只会在调试模式下被编译?
If so, is there any way to obtain this functionality on a custom class, i.e., a static call would only be compiled in if we are in Debug mode?
推荐答案
p>它被称为 ConditionalAttribute ,它已经在那里: Debug.WriteLine()
在释放模式下编译时,调用完全被删除。
It is called ConditionalAttribute and it is already there: Debug.WriteLine()
calls are removed entirely when you compile in Release mode.
它被声明为这样:
[ConditionalAttribute("DEBUG")]
public static void WriteLine(string message)
所以如果 DEBUG
符号未声明,例如,在版本构建的默认配置中。 (您可以更改在项目属性中为不同构建配置定义的预处理器符号。)
So any calls to it are removed if the DEBUG
symbol is not declared, e.g., in the default configuration of a release build. (You can change what pre-processor symbols are defined for different build configurations in the project properties.)
同样的(几乎?)每个方法在 Debug
。实际上,这是 Debug
和 Trace
- 跟踪
的方法也保留发布。
The same is true for (almost?) every method in Debug
. In fact, it is the main difference between Debug
and Trace
- Trace
's methods stay in release also.
这篇关于C#当Debug语句在Release模式下运行时被编译出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!