Scott Hanselman

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

August 30, '06 Comments [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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Wednesday, August 30, 2006 11:41:46 PM UTC
Your Main method has a return type of void but you return 2?
Thursday, August 31, 2006 8:19:28 AM UTC
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)
AndyToo
Thursday, August 31, 2006 10:00:51 AM UTC
Thanks, Scott! I was looking for the solution for a long time.
Thursday, August 31, 2006 11:56:38 AM UTC
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
Thursday, August 31, 2006 2:00:01 PM UTC
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.
Thursday, August 31, 2006 2:39:44 PM UTC
Jonas, it's inherited code, but I'll remind the author!
Scott Hanselman
Friday, September 01, 2006 6:21:10 PM UTC
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.
John
Friday, September 01, 2006 7:04:45 PM UTC
Sweet, I've wanted to know how do to this for ages...
Thanks Scott (and Nagaraj)!
-A
Friday, September 01, 2006 8:59:42 PM UTC
John - You're right...I got sloppy. I'll fix it.
Scott Hanselman
Friday, September 01, 2006 9:30:45 PM UTC
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.
John
Saturday, September 02, 2006 3:06:13 AM UTC
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
Saturday, September 02, 2006 6:45:25 PM UTC
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.