How to Programmatically Detect if an Assembly is Compiled in Debug or Release mode
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.
About Newsletter
AssemblyInfo.cs
----------------
#if DEBUG
[assembly: AssemblyTitle("My Cool App [Debug build]")]
#else
[assembly: AssemblyTitle("My Cool App [Retail]")]
#endif
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.
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.
Thanks Scott (and Nagaraj)!
-A
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.
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
#if (Debug || DEBUG)
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
Comments are closed.