有条件的DEBUG - 它是否仍然编译为RELEASE代码? [英] Conditional DEBUG - Does it still compile into RELEASE code?
问题描述
[条件(DEBUG)]
私人void DoSomeLocalDebugging()
{
//调试
}
是的,方法本身仍然是,但是你编译。
这是完全合乎逻辑的 - 因为点$ 条件
是依赖于在构建调用者时定义的预处理器符号,而不是当被调用者
简单测试 - 构建这个:
using System;
使用System.Diagnostics;
class Test
{
[Conditional(FOO)]
static void CallMe()
{
Console.WriteLine(称为);
}
static void Main()
{
CallMe();
}
}
运行代码(不定义FOO)看到没有输出,但是如果你看反射器,你会看到方法仍然存在。
换句话说:你认为.NET 发布的程序集(我们编译的程序集)是用DEBUG符号定义的?如果他们不是(我强烈怀疑他们不是!)我们如何可以调用 Debug.Assert
等?
诚然,当您正在构建私人方法时,不要包含它是有意义的 - 但是您可以看到,它仍然是内置的 - 这是合理的简单和一致。
I know that if I mark code as DEBUG code it won't run in RELEASE mode, but does it still get compiled into an assembly? I just wanna make sure my assembly isn't bloated by extra methods.
[Conditional(DEBUG)]
private void DoSomeLocalDebugging()
{
//debugging
}
Yes, the method itself still is built however you compile.
This is entirely logical - because the point of Conditional
is to depend on the preprocessor symbols defined when the caller is built, not when the callee is built.
Simple test - build this:
using System;
using System.Diagnostics;
class Test
{
[Conditional("FOO")]
static void CallMe()
{
Console.WriteLine("Called");
}
static void Main()
{
CallMe();
}
}
Run the code (without defining FOO) and you'll see there's no output, but if you look in Reflector you'll see the method is still there.
To put it another way: do you think the .NET released assemblies (the ones we compile against) are built with the DEBUG symbol defined? If they're not (and I strongly suspect they're not!) how would we be able to call Debug.Assert
etc?
Admittedly when you're building private methods it would make sense not to include it - but as you can see, it still is built - which is reasonable for simplicity and consistency.
这篇关于有条件的DEBUG - 它是否仍然编译为RELEASE代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!