Programmatically detecting Release/Debug mode (.NET)

.NetDebuggingRelease

.Net Problem Overview


> 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?

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - .NetDavy LandmanView Answer on Stackoverflow
Solution 2 - .NetJhonny D. Cano -Leftware-View Answer on Stackoverflow