Click here to Skip to main content
15,868,141 members
Articles / General Programming / Debugging
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
4 Feb 2012CPOL2 min read 39.6K   10   5
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:

C#
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)


Written By
Product Manager 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.

Comments and Discussions

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

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

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