Rilevamento a livello di codice della modalità di rilascio / debug (.NET) [duplicato]

StackOverflow https://stackoverflow.com/questions/654450

  •  19-08-2019
  •  | 
  •  

Domanda

  

Possibile duplicato:
   Come scoprire se un assembly .NET è stato compilato con il flag TRACE o DEBUG

  

Possibile duplicato:
   Come identificare se la DLL è il debug o Release build (in .NET)

Qual è il modo più semplice per verificare a livello di codice se l'assembly corrente è stato compilato in modalità Debug o Release?

È stato utile?

Soluzione

bool isDebugMode = false;
#if DEBUG
isDebugMode = true;
#endif

Se vuoi programmare un comportamento diverso tra debug e build di rilascio dovresti farlo in questo modo:

#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 se vuoi fare alcuni controlli sulle versioni di debug delle funzioni puoi farlo in questo modo:

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

Il Debug.Assert non sarà incluso nella build di rilascio.

Altri suggerimenti

Spero che ti sia utile:

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top