以编程方式检测发布/调试模式 (.NET) [英] Programmatically detecting Release/Debug mode (.NET)

查看:28
本文介绍了以编程方式检测发布/调试模式 (.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
如何确定 .NET 程序集是否使用 TRACE 或 DEBUG 标志编译

<块引用>

可能的重复:
如何识别 DLL是调试或发布版本(在 .NET 中)

以编程方式检查当前程序集是在调试模式还是发布模式下编译的最简单方法是什么?

解决方案

bool isDebugMode = false;#if 调试isDebugMode = true;#万一

如果你想在调试和发布版本之间编写不同的行为,你应该这样做:

#if DEBUGint[] 数据 = 新的 int[] {1, 2, 3, 4};#别的int[] 数据 = GetInputData();#万一int sum = data[0];for (int i= 1; i < data.Length; i++){总和 += 数据[i];}

或者,如果您想对函数的调试版本进行某些检查,您可以这样做:

public int Sum(int[] data){Debug.Assert(data.Length > 0);int sum = data[0];for (int i= 1; i < data.Length; i++){总和 += 数据[i];}返还金额;}

Debug.Assert 将不会包含在发布版本中.

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?

解决方案

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.Assert will not be included in the release build.

这篇关于以编程方式检测发布/调试模式 (.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆