<强>可能重复:结果   如何找出是否一个.NET组件与TRACE或调试标志编译

  

<强>可能重复:结果   如果DLL如何idenfiy是调试或发布版本(在.NET)

什么是或以编程方式检查当前组件在调试编译发布模式最简单的方法?

有帮助吗?

解决方案

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

如果您要调试和发布版本,你应该做这样的之间不同的行为进行编程:

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

或者,如果你想要做的功能调试版本某些检查,你可以做这样的:

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

Debug.Assert将不被包括在发布版本。

其他提示

我希望这对您有用:

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top