Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if calling assembly is in DEBUG mode from compiled referenced lib

Tags:

c#

I have a referenced library, inside there I want to perform a different action if the assembly that references it is in DEBUG/RELEASE mode.

Is it possible to switch on the condition that the calling assembly is in DEBUG/RELEASE mode?

Is there a way to do this without resorting to something like:

bool debug = false;

#if DEBUG
debug = true;
#endif

referencedlib.someclass.debug = debug;

The referencing assembly will always be the starting point of the application (i.e. web application.

like image 846
Andrew Bullock Avatar asked Oct 07 '10 09:10

Andrew Bullock


People also ask

How do I know if a DLL is Debug or Release?

One way that could work for most people is to simply open the DLL/EXE file with Notepad, and look for a path, for example search for "C:\" and you might find a path such as "C:\Source\myapp\obj\x64\Release\myapp. pdb", the "Release" shows that the build was done with Release configuration.


1 Answers

The accepted answer is correct. Here's an alternative version that skips the iteration stage and is provided as an extension method:

public static class AssemblyExtensions
{
    public static bool IsDebugBuild(this Assembly assembly)
    {
        if (assembly == null)
        {
            throw new ArgumentNullException(nameof(assembly));
        }

        return assembly.GetCustomAttribute<DebuggableAttribute>()?.IsJITTrackingEnabled ?? false;
    }
}
like image 151
Steve Cadwallader Avatar answered Sep 30 '22 18:09

Steve Cadwallader