views:

1147

answers:

4

What's the easiest way to programatically check if the current assembly was compiled in Debug or Release mode?

+14  A: 
Boolean isDebugMode = false;
#if DEBUG
isDebugMode = true;
#endif

If you want to program different behavior between debug and release builds you should do it like this:

#if DEBUG
   int[] data = new int[] {1, 2, 3, 4};
#else
   int[] data = GetInputData();
#endif
   int sum = data[0];
   for (int i= 1; i < data.Length; i++)
   {
     sum += data[i];
   }

Or if you want to do certain checks on debug versions of functions you could do it like this:

public int Sum(int[] data)
{
   Debug.Assert(data.Length > 0);
   int sum = data[0];
   for (int i= 1; i < data.Length; i++)
   {
     sum += data[i];
   }
   return sum;
}

The Debug.Assert will not be included in the release build.

Davy Landman
+1  A: 

I hope this be useful for you:

public static bool IsRelease(Assembly assembly) {
    object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true);
    if (attributes == null || attributes.Length == 0)
     return true;

    var d = (DebuggableAttribute)attributes[0];
    if ((d.DebuggingFlags & DebuggableAttribute.DebuggingModes.Default) == DebuggableAttribute.DebuggingModes.None)
     return true;

    return false;
}

public static bool IsDebug(Assembly assembly) {
    object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true);
    if (attributes == null || attributes.Length == 0)
     return true;

    var d = (DebuggableAttribute)attributes[0];
    if (d.IsJITTrackingEnabled) return true;
    return false;
}
Jhonny D. Cano -Leftware-
+1  A: 

You can download a portable utility to identify the same. The application is based on Scott hanselman's post at http://tinyurl.com/ydasofx .

The code file you can see at http://code.google.com/p/assemblyspy/source/browse/trunk/AssemblySpy/DebugOrRelease/AssemblySpy.cs

Download the tool here: http://assemblyspy.googlecode.com/files/DebugOrRelease.exe

Vimal Raj
A: 

The DebugOrRelease tool is very nice but doesn't seem to work with Mixed-Mode assemblies. Any ideas on how we can detect Debug or Release in a mixed-mode assembly?