68

Possible Duplicate:
How to find out if a .NET assembly was compiled with the TRACE or DEBUG flag

Possible Duplicate:
How to idenfiy if the DLL is Debug or Release build (in .NET)

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

Community
  • 1
  • 1
ripper234
  • 222,824
  • 274
  • 634
  • 905

2 Answers2

144
bool 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.

Aleš Doganoc
  • 11,568
  • 24
  • 40
Davy Landman
  • 15,109
  • 6
  • 49
  • 73
  • Is the OP asking about JIT Optimized build? If so, then this answer is incorrect. The Debug attribute can be declared in a JIT Optimized build or non-optimized. – Dave Black Jan 17 '17 at 16:06
15

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-
  • 17,663
  • 14
  • 81
  • 103
  • 4
    why both function has this line: if (attributes == null || attributes.Length == 0) return true; Somthing is wrong with this code. I did +1 it as the answer provides a real programmatic way rather than syntextual way to get the flag. sometime threre is a need to know if wer'e in debug mode being expressed as part of the code itself rather than a compiler flag. – G.Y Apr 23 '12 at 08:24
  • 2
    The DebuggableAttribute is present if you compile in Release mode and choose DebugOutput to anything other than "none". So, this answer is not correct. It doesn't even look for the JIT Optimization flag. Refer to my post on how to tell the difference both manually and programatically - http://dave-black.blogspot.com/2011/12/how-to-tell-if-assembly-is-debug-or.html – Dave Black Jan 17 '17 at 16:04
  • 5
    I defer to @DaveB on the difficulties of this in the general case. However, your question was a broad one and, if you are simply wanting to have your code behave differently when you are testing, I find this test useful (in VB.Net) `If System.Diagnostics.Debugger.IsAttached Then DoSomething '(Such as have a Form Behave Differently)` – Neil Dunlop Feb 04 '17 at 13:26
  • Debuggers don't always have to be attached, there are plenty of scenarios where you compile in debug mode, but attach the debugger later during execution. Also most process-dump utilities (think: production support against Release builds) attach as debuggers. They aren't the same as debug-mode by any stretch, nor is this a "safe" indication a program is being tested. – McGuireV10 Jul 04 '23 at 13:05