以编程方式检测发布/调试模式 (.NET)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/654450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically detecting Release/Debug mode (.NET)
提问by ripper234
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?
以编程方式检查当前程序集是在调试模式还是发布模式下编译的最简单方法是什么?
回答by Davy Landman
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.Assertwill not be included in the release build.
将Debug.Assert不会被包含在发布版本。
回答by Jhonny D. Cano -Leftware-
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;
}

