Determine if an assembly is a debug or release build

*This code originally came from another blog, although it was written in VB over there. It has lost some elegance in the translation, but I needed a very quick solution this afternoon, and I needed it in C#, so here you go. *

I needed to determine whether several assemblies were built as “Debug” or “Release” code this afternoon. They didn’t have any debug symbols with them, so that was no help. A bit of searching led me to the code below.

private void testfile(string file)
{
	if(isAssemblyDebugBuild(file))
	{
		MessageBox.Show(String.Format("{0} seems to be a debug build",file));
	}
	else
	{
		MessageBox.Show(String.Format("{0} seems to be a release build",file));
	}
}

private bool isAssemblyDebugBuild(string filename)
{
	return isAssemblyDebugBuild(System.Reflection.Assembly.LoadFile(filename));
}

private bool isAssemblyDebugBuild(System.Reflection.Assembly assemb)
{
	bool retVal = false;
	foreach(object att in assemb.GetCustomAttributes(false))
	{
		if(att.GetType() == System.Type.GetType("System.Diagnostics.DebuggableAttribute"))
		{
			retVal = ((System.Diagnostics.DebuggableAttribute)att).IsJITTrackingEnabled;
		}
	}
	return retVal;
}

The overloaded “isAssemblyDebugBuild” method will accept either a string containing the fiull filename, or a System.Reflection.Assembly object. It will return its best guess as to whether the the assembly is a debug build, as a boolean.

To call the methods as they are (to get a MessageBox), just use something like

testfile(@"C:\myUnknownAssembly.dll");

or access the underlying methods directly :

bool isDebug = isAssemblyDebugBuild(@"C:\myUnkownAssembly.dll");

I hope someone can use this to save themselves some time.

One thought on “Determine if an assembly is a debug or release build

Comments are closed.