Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

.NET - Determine Whether an Assembly was compiled in Debug Mode

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Apr 2010CPOL1 min read 21K   4   1
Finding out whether an assembly was compiled in Debug or Release mode is a task we must do from time to time...

Finding out whether an assembly was compiled in Debug or Release mode is a task we must do from time to time.

I know two ways to accomplish this:

Either attributes are applied to Assemblies and can be found in the Assembly Manifest but there are major differences between them:

  • AssemblyConfigurationAttribute must be added by the programmer but is human readable.
  • DebuggableAttribute is added automatically and is always present but is not human readable

You can easily get the Assembly Manifest by using the amazing ILDASM from your Visual Studio Studio Command Prompt:

ildasm_1

ildasm_2

And if you double click the MANIFEST item, you will get all manifest data.

Looking carefully, you will find the DebuggableAttribute:

ildasm_3

And perhaps the AssemblyConfigurationAttribute:

ildasm_4 

AssemblyConfigurationAttribute

Locate the AssemblyConfigurationAttribute – this attribute can be easily interpreted: its value can either be Debug or Release.

ildasm_5

DebuggableAttribute

If AssemblyConfigurationAttribute is not present, then we must use the DebuggableAttribute to achieve our goal.

Since we cannot understand the DebuggableAttribute value, we have to open the assembly from another tool and read this attribute content. There’s no such tool available out-of-the-box but we can easily create a Command Line tool and use a method similar to:

C#
private bool IsAssemblyDebugBuild(string filepath)
{    
    return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
}
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;
}

or (if you prefer LINQ):

C#
private bool IsAssemblyDebugBuild(Assembly assembly)
{    
    return assembly.GetCustomAttributes(false).Any(x => 
	(x as DebuggableAttribute) != null ? 
	(x as DebuggableAttribute).IsJITTrackingEnabled : false);
}

As you can see … it’s pretty simple.

Note

Typically, I add a pre-build task to my build server so that the AssemblyConfigurationAttribute is added to the CommonAssemblyInfo file with the appropriated value: either “Debug” or “Release”. This way anyone can easily see, using only the ILDASM, which kind of compilation was used to generate my assemblies.

License

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


Written By
Architect everis
Portugal Portugal
Over 13 years of experience in the Software Development working mainly in the banking and insurance industry.

Over 3 year of experience as Operations Team Leader focused on Infrastructure Management and Software Configuration Management.

I've been honored with the Microsoft Most Valuable Professional (MVP) Award for three consecutive years, 2010, 2011 and 2012, in recognition to exceptional technical contributions and leadership.

Current / Recent Technical Projects
- Dominican Republic Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Colombian SECOPII Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Main Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Development ecosystem management, including Server management, , capacity management, SW monitoring and deploy management

Areas of Specialization:
- Operations Management - ISO 20000 & ISO 27001 driven
- Team Management and Coaching
- Technology Leadership, Solutions/Architecture
- Product life cycle management, Continuous Integration
- Technological background in Microsoft frameworks and tools.

Comments and Discussions

 
QuestionReflector shows Debuggable attributes Pin
Michael Freidgeim16-Oct-11 14:20
Michael Freidgeim16-Oct-11 14:20 
RedGate tool Reflector show Debuggable attributes, e.g
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
From Reflector I also found that

public bool IsJITTrackingEnabled
{
get
{
return ((this.m_debuggingModes & DebuggingModes.Default) != DebuggingModes.None);
}
}
and
public enum DebuggingModes
{
Default = 1,
DisableOptimizations = 0x100,
EnableEditAndContinue = 4,
IgnoreSymbolStoreSequencePoints = 2,
None = 0
}

So IsJITTrackingEnabled=((2 & 1)!= 0) =false, so debugging was disabled


Also ILDASM for my .EXE (build in .Net 4.)shows
.custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )
-Neither DEBUG not RELEASE
Michael Freidgeim.
Blog: http://geekswithblogs.net/mnf/

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.