需要.NET code到只有在调试配置执行 [英] Need .NET code to execute only when in debug configuration

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

问题描述

我有访问API了网络上的一些code。其中一个API的参数,可以让我让他们知道我正在测试。

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.

我想只设置这个参数在我的code时,我测试。目前,我只评论了code,当我做一个发布版本。

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:可以使用的 条件 属性:

1: You can use Conditional attribute:

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

2: #如果 preprocessor指令

#if DEBUG
    static int testCounter = 0;
#endif 

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

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.

了解更多:

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