Conditional DEBUG - Does it still compile into RELEASE code?

故事扮演 提交于 2019-12-07 02:44:15

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/3477364/conditional-debug-does-it-still-compile-into-release-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!