如何查找代码是否在调试模式下运行 [英] How to find if code is running in Debug mode

查看:85
本文介绍了如何查找代码是否在调试模式下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


仅当代码在调试模式下运行时,才需要执行一些代码块.
我不知道该如何实现,您能说明一下吗?

我用Google搜索了很多页面,但发现没有用.


Prathamesh

Hi,
I need to Execute some block of code only if Code is running in Debug Mode.
I dont know how to achieve this, Can you illustrate it ??

I googled Many pages but found none useful.


Prathamesh

推荐答案

请查看Prera​​k的解决方案.有效而实用地,它可能可以解决您的问题.

但是,这不是您真正要求的.您问只有在调试模式下运行代码时,我才需要执行一些代码块".如果您使用Prera​​k指出的解决方案,您会得到一些不同:您将能够执行或不执行某些代码片段,但这仅仅是因为您可以拥有两种不同的代码版本:一种是在执行片段时,而另一种则不是.编译应用程序后,它将以一种或另一种方式运行.

换句话说,您不能以一种或另一种模式"运行您的应用程序.相反,您只能创建两个不同的应用程序,它们对于两种不同的配置的行为会有所不同.一个重要的节点:调试只是一个配置,与调试本身无关.与调试有关的是编译选项.您可以根据需要拥有任意数量的不同配置.配置只是以某种名称命名的某些配置的保护伞;您手动为每个配置配置选项,这些选项可能与调试无关,也可能与调试无关.就是说:小心仅在某些配置中执行的代码;它可能是测试结果不足的原因.
换句话说,您无法确定代码是否在调试模式下运行,因为这种模式不存在.

惊喜? :-)

—SA
Please look at the solution by Prerak. Effectively and practically, it probably solves your problem.

However, this is not what you literally asked for. You asked "I need to execute some block of code only if code is running in Debug Mode". If you use the solution pointed out by Prerak, you will get something different: you will be able to execute or not execute some fragment of code, but only because you can have two different builds of you code: one when your fragment is executed, and one when it is not. Once your application is compiled, it will behave either in one or another way.

In other words, you cannot run your application in one or another "mode"; instead, you only can create two different applications which behave differently for two different configuration. One important node: the Debug is only a configuration and has nothing to do with debugging itself. What does have something to do with debugging is the compilation options. You can have as many different configurations as you want; a configuration is just an umbrella for some configuration set under a certain name; you manually configure options for each configuration, and these options may or may not be related to debugging. That said: be careful with the code which executes only in certain configuration; it can be a source of inadequate testing results.

In other words, you cannot determine if a code is running in Debug Mode because such mode does not exist.

Surprise? :-)

—SA


使用类似
的指令
Use directives like
#if DEBUG
    Console.WriteLine("Debug version");
#endif


阅读:
http://stackoverflow.com/questions/104235/how-can-i- use-debugbreak-in-c


这篇关于如何查找代码是否在调试模式下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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