Detección programática del modo Release / Debug (.NET) [duplicado]
Pregunta
Posible duplicado:
Cómo saber si un ensamblado .NET se compiló con el indicador TRACE o DEBUG
Posible duplicado:
Cómo identificar si la DLL es depurar o liberar compilación (en .NET)
¿Cuál es la forma más fácil de verificar mediante programación si el ensamblaje actual se compiló en modo de depuración o liberación?
Solución
bool isDebugMode = false;
#if DEBUG
isDebugMode = true;
#endif
Si desea programar un comportamiento diferente entre las versiones de depuración y lanzamiento, debe hacerlo así:
#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];
}
O si desea hacer ciertas comprobaciones en las versiones de depuración de funciones, puede hacerlo así:
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;
}
El Debug.Assert
no se incluirá en la versión de lanzamiento.
Otros consejos
Espero que esto sea útil para usted:
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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow