"调试只"代码应该只有当&QUOT运行;开启" [英] "Debug only" code that should run only when "turned on"

查看:218
本文介绍了"调试只"代码应该只有当&QUOT运行;开启"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充一点,只有当人请求调试运行一些C#调试只有代码。在C ++中,我用来做类似下面的内容:

 无效美孚()
{
// ...
的#ifdef DEBUG
静态布尔s_bDoDebugOnlyCode = FALSE;
如果(s_bDoDebugOnlyCode)
{
//调试这里只代码当人调试
//手动设置上面真布尔被执行。然后,它停留在会议休息
//直到他们将其设置为false。
}
#ENDIF
// ...
}

我不能做在C#中完全相同的,因为没有本地静态



:什么是最好的方法要做到这一点在C#?




  1. 我应该使用私有类静态字段用C#预处理指令(#如果/#ENDIF DEBUG)<? / LI>
  2. 我应该使用条件属性(持有码),然后私有类静态字段(不可以由C#预处理指令#包围的if /#ENDIF DEBUG ?)。

  3. 别的东西吗?


解决方案

实例变量可能会是你想要的东西的方式。你可以把静态坚持该计划的寿命相同的值(或者根据你的静态内存模型线程),或者让它作为一个普通实例VAR来控制它在一个对象实例的生命。如果该实例是一个单,他们会表现以同样的方式。

 #如果DEBUG 
私人/ *静态* /布尔s_bDoDebugOnlyCode = FALSE;
#ENDIF

无效美孚()
{
// ...
#如果DEBUG
如果(s_bDoDebugOnlyCode)
{当用DEBUG常数,
//当人的手动调试上面设置为true的布尔编译
//代码在这里只能得到执行。
//然后它停留在会议休息,直到他们将其设置为false。
}
#ENDIF
// ...
}

只是要完整,语法(预处理指令)被认为有点杂牌组装电脑的使用来控制程序流程。 .NET已经内置在回答这个问题的一半,采用了条件属性。

 私人/ * *静/布尔doDebugOnlyCode = FALSE; 
[条件(DEBUG)]
无效美孚()
{
// ...
如果(doDebugOnlyCode)
{$ B $当用DEBUG常数,
//当人的手动调试上面设置为true的布尔编译b //代码在这里只能得到执行。
//然后它停留在会议休息,直到他们将其设置为false。
}
// ...
}

没有编译指示,干净多了。不足之处是有条件的,只能应用于方法,所以你必须要处理布尔变量没有在发布版本做任何事情。由于变量的存在只是为了从VS执行主机切换,并在一份新闻稿中建立其价值并不重要,这是相当无害的。


I would like to add some C# "debug only" code that only runs if the person debugging requests it. In C++, I used to do something similar to the following:

void foo()
{   
  // ...
#ifdef DEBUG
  static bool s_bDoDebugOnlyCode = false;
  if (s_bDoDebugOnlyCode)
  {
      // Debug only code here gets executed when the person debugging 
      // manually sets the bool above to true.  It then stays for the rest
      // of the session until they set it to false.
  }
#endif
 // ...
}

I can't do exactly the same in C# since there is no local statics.

Question: What is the best way to accomplish this in C#?

  1. Should I use a private class static field with C# preprocessor directives (#if/#endif DEBUG)?
  2. Should I use the Conditional attribute (to hold the code), and then a private class static field (not surrounded by C# preprocessor directives #if/#endif DEBUG?).
  3. Something else?

解决方案

An instance variable would probably be the way to do what you want. You could make it static to persist the same value for the life of the program (or thread depending on your static memory model), or make it an ordinary instance var to control it over the life of an object instance. If that instance is a singleton, they'll behave the same way.

#if DEBUG
private /*static*/ bool s_bDoDebugOnlyCode = false;
#endif

void foo()
{   
  // ...
#if DEBUG
  if (s_bDoDebugOnlyCode)
  {
      // Code here gets executed only when compiled with the DEBUG constant, 
      // and when the person debugging manually sets the bool above to true.  
      // It then stays for the rest of the session until they set it to false.
  }
#endif
 // ...
}

Just to be complete, pragmas (preprocessor directives) are considered a bit of a kludge to use to control program flow. .NET has a built-in answer for half of this problem, using the "Conditional" attribute.

private /*static*/ bool doDebugOnlyCode = false; 
[Conditional("DEBUG")]
void foo()
{   
  // ...    
  if (doDebugOnlyCode)
  {
      // Code here gets executed only when compiled with the DEBUG constant, 
      // and when the person debugging manually sets the bool above to true.  
      // It then stays for the rest of the session until they set it to false.
  }    
  // ...
}

No pragmas, much cleaner. The downside is that Conditional can only be applied to methods, so you'll have to deal with a boolean variable that doesn't do anything in a release build. As the variable exists solely to be toggled from the VS execution host, and in a release build its value doesn't matter, it's pretty harmless.

这篇关于&QUOT;调试只&QUOT;代码应该只有当&QUOT运行;开启&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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