Know whether your assembly is Debug or Release

Sometimes, you want to know if your assembly is a Debug or Release build – like if you’re outputting assembly information for diagnostic purposes. But how do you know?

Well, the easiest way would be to use C# Preprocessor Directives to change what your application does:

bool isDebug = false;
#if (DEBUG)
isDebug = true;
isDebug = false;

I know the else isn’t needed, but it’s to show the other condition too.

So, what is this magical value DEBUG? Well, it’s a preprocessor variable that is optionally added to your project at build time. By default, it’s true for Debug build, and not for Release builds. You can find it in the project settings:

You could also just put this information into the AssemblyInfo.cs file for your assembly with the same Preprocessor directives:

[assembly: AssemblyTitle("Example Assembly")]
#if (DEBUG)
    [assembly: AssemblyConfiguration("Debug")]
    [assembly: AssemblyProduct("Example Assembly (Debug)")]
    [assembly: AssemblyConfiguration("Release")]
    [assembly: AssemblyProduct("Example Assembly (Release)")]

Here I’m using AssemblyProduct to show the version as this information is shown in Details tab of the file’s properties in Windows:

Okay, so we can have the solution compile differently for Release and Debug. What if we wanted to detect it for an assembly that we’d not compiled? Well, Scott Hanselman has answered this already, but he doesn’t really define what is meant by a Debug build? Dave Black goes into this in more detail – but in short, does ‘Debug’ mean you can attach a debugger, or that optimisation of the code is turned off? I agree with his opinion – most people are asking if the code is optimized.

So, based on their offerings, here is my code for determining if an assembly was build in ‘Debug’ mode or not:

public static bool IsAssemblyDebug(Assembly assm)
    bool IsDebug = false;
    object[] attributes = assm.GetCustomAttributes(typeof(DebuggableAttribute), false);
    if (attributes.Length > 0)
        DebuggableAttribute d = (DebuggableAttribute)attributes[0];
        IsDebug = d.IsJITOptimizerDisabled;
    return IsDebug;
Know whether your assembly is Debug or Release

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.