How to determine if the calling assembly is in DEBUG mode from a compiled lib link

I have a help library inside which I want to perform another action if the assembly that references it is in DEBUG / RELEASE mode.

Is it possible to include 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 assembly reference will always be the starting point of the application (i.e. web application.

+5
source share
3 answers

Google says it is very simple. You get information from the DebuggableAttribute of this assembly:

IsAssemblyDebugBuild(Assembly.GetCallingAssembly());

private bool IsAssemblyDebugBuild(Assembly assembly)
{
    foreach (var attribute in assembly.GetCustomAttributes(false))
    {
        var debuggableAttribute = attribute as DebuggableAttribute;
        if(debuggableAttribute != null)
        {
            return debuggableAttribute.IsJITTrackingEnabled;
        }
    }
    return false;
}
+8
source

reflection, , , .

+1

The accepted answer is correct. Here's an alternative version that skips the iteration step 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;
    }
}
0
source

All Articles