Scott Hanselman

How to Programmatically Detect if an Assembly is Compiled in Debug or Release mode

August 31, 2006 Comment on this post [12] Posted in Programming
Sponsored By

Nagaraj from my company made this little util recently to run against a compiled assembly and see if it is a Debug or Release version. I added the DOS ErrorLevel return codes.

using System;

using System.IO;

using System.Diagnostics;

using System.Reflection;

 

namespace Foo.Tools

{

    class BuildFind

    {

        public static int GetBuildType(string AssemblyName)

        {

            Assembly assm = Assembly.LoadFrom(AssemblyName);

            object[] attributes = assm.GetCustomAttributes(typeof(DebuggableAttribute), false);

 

            if (attributes.Length == 0)

            {

                Console.WriteLine(String.Format("{0} is a RELEASE Build....", AssemblyName));

                return 0;

            }

            foreach (Attribute attr in attributes)

            {

                if (attr is DebuggableAttribute)

                {

                    DebuggableAttribute d = attr as DebuggableAttribute;

                    Console.WriteLine(
                       String.Format("Run time Optimizer is enabled : {0}", !d.IsJITOptimizerDisabled));

                    Console.WriteLine(
                        String.Format("Run time Tracking is enabled : {0}", d.IsJITTrackingEnabled));

                    if (d.IsJITOptimizerDisabled == true)

                    {

                        Console.WriteLine(String.Format("{0} is a DEBUG Build....", AssemblyName));

                        return 1;

                    }

                    else

                    {

                        Console.WriteLine(String.Format("{0} is a RELEASE Build....", AssemblyName));

                        return 0;

                    }

                }

            }

            return 3;

        }

 

        [STAThread]

        static int Main(string[] args)

        {

            if (args.Length == 0)

            {

                Console.WriteLine("Usage GetBuildType <assemblyName>");

                return 2;

            }

            return BuildFind.GetBuildType(args[0]);

        }

    }

}

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
August 31, 2006 3:41
Your Main method has a return type of void but you return 2?
August 31, 2006 12:19
Nice. Have you seen Jeff Key's 'IsDebug' tool? (www.sliver.com) It does the same sort of thing, and comes with source. Gotta love Jeff Key. Very handy tool for discovering that most of your developers have been deploying debug assemblies to production... (slapped wrist)
August 31, 2006 14:00
Thanks, Scott! I was looking for the solution for a long time.
August 31, 2006 15:56
And for your own assemblies do everyone a favour and:...

AssemblyInfo.cs
----------------

#if DEBUG
[assembly: AssemblyTitle("My Cool App [Debug build]")]
#else
[assembly: AssemblyTitle("My Cool App [Retail]")]
#endif
August 31, 2006 18:00
Why the Console.WriteLine(String.Format("...."))?

Console.WriteLine has string formating built in.. So you could just do:

Console.WriteLine("{0} is debug..", Assemblyname);

Anyhow, nifty little tool. Actually discussed this with my co-worker the other day, as we're moving into the final stage of the development cycle we want to automate the deployment into the production environment, and only want to deploy RELEASE assmelbies.
August 31, 2006 18:39
Jonas, it's inherited code, but I'll remind the author!
September 01, 2006 22:21
Scott,

I know it is inherited code, but I am wondering about the return codes.

Seems like you can get console output indicating DEBUG and return code 1.
Then seems like you can get console output indicating RELEASE and return codes 0 or 1.

So it seems like you would never *know* that it was a "DEBUG" build. Then again
I could easily have missed something :-)

John.
September 01, 2006 23:04
Sweet, I've wanted to know how do to this for ages...
Thanks Scott (and Nagaraj)!
-A
September 02, 2006 0:59
John - You're right...I got sloppy. I'll fix it.
September 02, 2006 1:30
Scott

No worries.

I guess only bottom (command line) dwellers would care. Talking of which, it seems a perfect candidate for some PowerShell magic. Maybe even making the script follow dependencies. Dependencies would be easier as you could handle an array in PS. That way, you could see if your entire product had any debug components.

Just a thought :-)

John.
September 02, 2006 7:06
A few days ago I wrote a Powershell script to do the same thing. Hopy some people will find it usefull.

http://guessman.blogspot.com/2006/08/first-time-scripting-with-powershell.html

function IsDebug() {
process
{
trap {continue;}
$asmb = $null;
$asmb = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($_.FullName);
if ($asmb)
{
$Attribs = [System.Reflection.CustomAttributeData]::GetCustomAttributes($asmb);
foreach ($attrib in $Attribs)
{
if ($attrib.ToString().StartsWith("[System.Diagnostics.DebuggableAttribute") -AND
-NOT $attrib.ToString().StartsWith("[System.Diagnostics.DebuggableAttribute((Boolean)False") )
{
$_.Name + " Is DEBUG " + $attrib.ToString();
}
}
}
}
}

===

dir | IsDebug($_)
===
Timur
September 02, 2006 22:45
For my part, I always make sure that the following snippet of code is in my AssemblyInfo.cs:

#if (Debug || DEBUG)
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.