需要.NET code到只有在调试配置执行 [英] Need .NET code to execute only when in debug configuration
问题描述
我有访问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.
了解更多:
- 埃里克利珀。 <一href="http://ericlippert.com/2009/09/10/whats-the-difference-between-conditional-compilation-and-the-conditional-attribute/">What's条件编译和条件属性之间的区别是什么?
- C#程序员参考:有条件的方法教程
- 比尔·瓦格纳。 有效的C#:50具体的方法来提高你的C#(书),章的使用条件属性而不是#如果的
- 约翰·罗宾斯。 断言和.NET 追踪
- 山姆·艾伦。 点不皮尔斯的
- Eric Lippert. What's the difference between conditional compilation and the conditional attribute?
- C# Programmer's Reference: Conditional Methods Tutorial
- Bill Wagner. Effective C#: 50 Specific Ways to Improve Your C# (book), chapter: Use Conditional Attributes Instead of #if
- John Robbins. Assertions and Tracing in .NET
- Sam Allen. Dot Not Perls
- C# Conditional Attribute, DEBUG Array
- C# Debug.Write Methods
还有一个工具, DebugView中,该允许捕捉从外部应用程序的调试信息
There is also a tool, DebugView, which allow to capture debug information from external applications.
这篇关于需要.NET code到只有在调试配置执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!