Click here to Skip to main content
Click here to Skip to main content

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

By , 4 Feb 2012
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

Huisheng Chen
Architect 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.
Follow on   Twitter

Comments and Discussions

 
QuestionThoughts PinmemberPIEBALDconsult2-Feb-12 2:23 
AnswerRe: Thoughts PinmemberHuisheng Chen2-Feb-12 10:23 
GeneralRe: Thoughts PinmemberPIEBALDconsult3-Feb-12 2:25 
GeneralRe: Thoughts PinmemberHuisheng 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 | Mobile
Web02 | 2.8.140415.2 | Last Updated 4 Feb 2012
Article Copyright 2012 by Huisheng Chen
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid