以编程方式检查当前程序集是在调试还是发布模式下编译的最简单方法是什么?
ripper234
问问题
63305 次
2 回答
135
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
不会包含在发布版本中。
于 2009-03-17T14:21:30.540 回答
15
我希望这对你有用:
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;
}
于 2009-03-17T14:22:17.027 回答