Click here to Skip to main content
11,709,955 members (73,615 online)
Click here to Skip to main content

To tell if an assembly is compiled in debug or release mode in c#

, 4 Feb 2012 CPOL 18.2K 9
Rate this:
Please Sign up or sign in to vote.
Using reflection to dynamically verify if an assembly is in debug or release compilation
I have seen quite a few articles or tips talking about how to find out if an assembly is in debug mode or not (in the assembly), like this: (C#) Determining whether the current build mode is Debug or Release[^]

But there are scenarios that we need to tell an assembly is compiled as debug or release configuration (out of the assembly).

Because it is a small function, I try to put intact reference so that I do not include namespace:

public static bool IsInDebugMode(string FileName)
{
    return IsInDebugMode(FileName, false);
}
 
public static bool IsInDebugMode(string FileName, bool IsAssemlbyName)
{
    System.Reflection.Assembly assembly;
    if (IsAssemlbyName)
        assembly = System.Reflection.Assembly.Load(FileName);
    else
        assembly = System.Reflection.Assembly.LoadFile(FileName);
    return IsInDebugMode(assembly);
}
 
public static bool IsInDebugMode(System.Reflection.Assembly Assembly)
{
    var attributes = Assembly.GetCustomAttributes(typeof(System.Diagnostics.DebuggableAttribute), false);
    if (attributes.Length > 0)
    {
        var debuggable = attributes[0] as System.Diagnostics.DebuggableAttribute;
        if (debuggable != null)
            return (debuggable.DebuggingFlags & System.Diagnostics.DebuggableAttribute.DebuggingModes.Default) == System.Diagnostics.DebuggableAttribute.DebuggingModes.Default;
        else
            return false;
    }
    else
        return false;
}

The function is universal, works for library/gui/cui(console) assemblies, does not require developer to put customized property or functions to indicate.

You may wonder how I got this solution. I believe that the compiler must store some information in the assembly so that it can tell the debugging is supported or not.

First I run the IL DASM, open a debug as well as release version of a hello world application side by side, and compare the manifest, I found some differences like:

--debug version

// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )

--release version

// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )

Immediately, I figured out that we can use reflection to get the System.Diagnostics.DebuggableAttribute custom attribute.

After some testing for GUI/CUI/Library applications, I believe that System.Diagnostics.DebuggableAttribute.DebuggingModes.Default is the one to distinguish between debug and release.

Details of the DebuggableAttribute value:
  1. debug mode:
    1. standard (Output = full), (01 00 07 01) = 263, Default | DisableOptimizations | IgnoreSymbolStoreSequencePoints | EnableEditAndContinue
    2. Project Settings -> Optimize code, (01 00 03 00) = 3, Default | IgnoreSymbolStoreSequencePoints
    3. Project Settings -> Advanced -> Output -> none, no custom attribute available (oops...)
    4. Project Settings -> Advanced -> Output -> pdb-only, no custom attribute available (oops...)
  2. release mode:
    1. standard (Output = pdb-only, Optimize Code), (01 00 02 00) = 2, IgnoreSymbolStoreSequencePoints
    2. Project Settings -> Advanced -> Output -> none, no custom attribute available (oops...)
    3. Project Settings -> Advanced -> Output -> full, (01 00 03 00) = 3, Default | IgnoreSymbolStoreSequencePoints
To sum up, as long as the user does not play with the "Output" method, we should be fairly sure if the application is being built in debug or release mode.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Huisheng Chen
Technical Lead www.xnlab.com
Australia Australia
I was born in the south of China, started to write GWBASIC code since 1993 when I was 13 years old, with professional .net(c#) and vb, founder of www.xnlab.com

Now I am living in Sydney, Australia.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sivaji156530-May-14 20:54
memberSivaji156530-May-14 20:54 
QuestionThoughts Pin
PIEBALDconsult2-Feb-12 2:23
memberPIEBALDconsult2-Feb-12 2:23 
AnswerRe: Thoughts Pin
Huisheng Chen2-Feb-12 10:23
memberHuisheng Chen2-Feb-12 10:23 
GeneralRe: Thoughts Pin
PIEBALDconsult3-Feb-12 2:25
memberPIEBALDconsult3-Feb-12 2:25 
GeneralRe: Thoughts Pin
Huisheng Chen3-Feb-12 10:19
memberHuisheng Chen3-Feb-12 10:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Feb 2012
Article Copyright 2012 by Huisheng Chen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid