Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can #if DEBUG be executed after building in Release?

Tags:

c#

There is a security discussion going-on at work as to whether the following DEPLOYED code below can be reached or "hoisted" into...even though it was built in RELEASE mode.

Thoughts?

EDIT:
I do "see" it in DotPeek - even after building in Release.

  • However, the file is "grayed-out"

  • Does that mean it won't execute?

  • DotPeek merely "decompiles" the code...it doesn't show you what code exists in the mode it is built-in...right?

THE CODE LOOKS LIKE:

    using System;
    using System.ServiceProcess;

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
#if DEBUG
            var myservice = new StpListener();
            myservice.OnDebug();

            //KEEP the service alive
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new StpListener() 
            };
            ServiceBase.Run(ServicesToRun);
#endif
        }
    } 
like image 479
Prisoner ZERO Avatar asked Nov 25 '25 12:11

Prisoner ZERO


2 Answers

Don't confuse debug/release configuration with conditional code. It is entirely possible to apply the DEBUG conditional attribute to a release mode config.

Look, I'm evil!

enter image description here

See: When #if DEBUG runs

like image 141
Jamiec Avatar answered Nov 28 '25 02:11

Jamiec


It's a compile time feature. Once the compiler has finished it's job, you'll either have the code between #if and #else or the code between #else and #endif.

You'll never (so long as you leave those items in) produce a binary containing both sets of code.

like image 34
Damien_The_Unbeliever Avatar answered Nov 28 '25 00:11

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!