条件调试 - 它是否仍然编译成发布代码?

发布于 2024-09-14 05:22:42 字数 190 浏览 5 评论 0原文

我知道,如果我将代码标记为 DEBUG 代码,它不会在 RELEASE 模式下运行,但它仍然会被编译成程序集吗?我只是想确保我的程序集不会因额外的方法而变得臃肿。

[Conditional(DEBUG)]
private void DoSomeLocalDebugging()
{
   //debugging
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-09-21 05:22:42

是的,无论您如何编译,方法本身仍然被构建。

这是完全合乎逻辑的 - 因为Conditional的要点是依赖于调用者构建时定义的预处理器符号,而不是被调用者构建时定义的预处理器符号。建造的。

简单测试 - 构建这个:

using System;
using System.Diagnostics;

class Test
{
    [Conditional("FOO")]
    static void CallMe()
    {
        Console.WriteLine("Called");
    }

    static void Main()
    {
        CallMe();
    }
}

运行代码(不定义 FOO),您将看到没有输出,但如果您查看 Reflector,您将看到该方法仍然存在。

换句话说:您认为 .NET 已发布的程序集(我们编译的程序集)是使用定义的 DEBUG 符号构建的吗?如果它们不是(我强烈怀疑它们不是!)我们如何能够调用 Debug.Assert 等?

诚然,当您构建私有方法时,不包含它是有意义的 - 但正如您所看到的,它仍然被构建 - 这对于简单性和一致性来说是合理的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文