Steve Smith's Blog

Musings on Software and the Developer Community

Determine Whether an Assembly was compiled in Debug Mode

I’m working on a little application right now that provides some insight into the assemblies in use for a given application.  One of the things that I want to be able to show is whether or not each assembly was built in Debug or Release mode.  As you’re no doubt aware, running applications in production that were built in Debug mode can be a major performance problem (at a minimum – depending on what else you have turned on in Debug mode it could also be a security issue).

So I did some binging (followed by some purging? no, wait, that with a hard G sound) and quickly found some sample code that I was able to use in some test code to confirm that it is working.  Fellow MVP Bill McCarthy wrote (like, 5 years ago to the day!) about how to do this in Visual Basic.  Not that I have anything against VB, but my preference since VB6 has been C# (if nothing else, it makes JavaScript much easier to grok), so I had to translate the code into my native tongue, which provides me with something to share with you.  This is literally Bill’s code, translated by me into C#.  Any bugs you can probably assign to me as the translator, rather than Bill as original author.

Check If An Assembly Was Compiled In Debug Mode

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

(subscribe to my blog)
(follow me on twitter)

    kick it on DotNetKicks.com

Wednesday, 17 June 2009

Comments

 avatar

Rennie Petersen said on 19 Jun 2009 at 6:25 AM

Sorry! Please delete the above two posts!

This should be OK, but it is debatable as to whether it is an improvement over the original.

private static bool IsAssemblyDebugBuild(Assembly asmbly)

{

object [] daa = asmbly.GetCustomAttributes(typeof (DebuggableAttribute), false);

if (daa.Length == 0)

return false;

return (daa[0] as DebuggableAttribute).IsJITTrackingEnabled;

}


 avatar

Rennie Petersen said on 19 Jun 2009 at 11:17 AM

One final suggestion, which can hopefully be considered to be an improvement of my previous version, at least:

private static bool IsAssemblyDebugBuild(Assembly asmbly)

{

object[] daa = asmbly.GetCustomAttributes(typeof(DebuggableAttribute), false);

return daa.Length > 0 && (daa[0] as DebuggableAttribute).IsJITTrackingEnabled;

}


 avatar

Donn Felker said on 19 Jun 2009 at 11:49 AM

Good tip! Thanks.

I changed it for my implementation as a LINQ one liner:

private bool IsAssemblyDebugBuild(Assembly assembly)

{

return assembly.GetCustomAttributes(false).Any(x => (x as DebuggableAttribute) != null ? (x as DebuggableAttribute).IsJITTrackingEnabled : false);

}