Find whether an assembly was compiled in Debug or Release mode

No.of Views746
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : Visual Studios , How to
Find whether an assembly was compiled in Debug or Release mode
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I know two ways of accomplish this:

  • Searching for the System.Diagnostics.DebuggableAttribute
  • Searching for the System.Reflection.AssemblyConfigurationAttribute
Either attributes are applied to Assemblies and can be found in the Assembly Manifest but there are a major difference 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:





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

Looking carefully you will find the DebuggableAttribute:



And perhaps the AssemblyConfigurationAttribute.

AssemblyConfigurationAttribute

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

DebuggableAttribute

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

Since we cannot understood 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:

{codecitation class="brush: csharp; gutter: true;" width="500px"}


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;
}

{/codecitation}

or (if you prefer LINQ)

{codecitation class="brush: csharp; gutter: true;" width="500px"}

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

Thank you

Amal


 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top