需要.NET代码才能在调试配置时执行 [英] Need .NET code to execute only when in debug configuration

查看:133
本文介绍了需要.NET代码才能在调试配置时执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以在网络上访问一个API。 API的参数之一就是让我知道我正在测试。



当我测试时,我只想在我的代码中设置这个参数。目前,我只是在发布版本时将代码评论。



有没有一种基于构建配置的自动方法?

解决方案

1:您可以使用 条件 属性:

  [Conditional(DEBUG)] 
static void Method(){}

2: #if 预处理指令

  #if DEBUG 
static int testCounter = 0;
#endif

3:或 Debug.Write \ Debug.WriteLine 方法:

  Debug.Write(在输出窗口中写入的东西); 






小心使用#如果指令,因为它可能会在版本构建中产生意外的情况。例如,请参阅:

  string sth = null; 
#if DEBUG
sth =哦,嗨!;
#endif
Console.WriteLine(sth);

在这种情况下,非调试版本将打印一个空白消息。






阅读更多:








还有一个工具, DebugView ,允许捕获调试来自外部应用程序的信息。


I have some code that access an API out on the web. One of the API's parameters allows me to let them know that I am testing.

I would like to only set this parameter in my code when I am testing. Currently, I just comment the code out when I do a release build.

Is there an automatic way of doing this based on the build configuration?

解决方案

1: You can use Conditional attribute:

[Conditional("DEBUG")]
static void Method() { } 

2: #if preprocessor directive:

#if DEBUG
    static int testCounter = 0;
#endif 

3: Or Debug.Write \ Debug.WriteLine method:

Debug.Write("Something to write in Output window.");


Beware of using #if directive since it can produce unintended situations in Release build. For example, see:

    string sth = null;
#if DEBUG
    sth = "oh, hi!";
#endif
    Console.WriteLine(sth);

In this case, non-Debug build will print a blank message.


Read more:


There is also a tool, DebugView, which allow to capture debug information from external applications.

这篇关于需要.NET代码才能在调试配置时执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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