在发布版本(C#)中时,如何停止对调试函数的调用中的参数求值 [英] How to stop evaluation of parameters in calls to debug functions when in Release build (C#)

查看:110
本文介绍了在发布版本(C#)中时,如何停止对调试函数的调用中的参数求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码充满了类似于以下的调试表达式:

I have some code which is littered with debug expressions similar to:

System.Diagnostics.Debug.WriteLine(System.BitConverter.ToString(data.Take(32).ToArray()));

如何防止发布版本评估 System.BitConverter.ToString (data.Take(32).ToArray())

What can I do to prevent the Release version evaluating System.BitConverter.ToString(data.Take(32).ToArray())?

我添加了自己的调试类,该类允许我控制对 WriteLine()基于我想要的报告级别,但是两种方法都可以评估Linq和字符串转换,即使在Release模式下也可以(不是)?

I have added my own debug class that allows me to control calls to WriteLine() based on the level of reporting I want but both methods evaluate the Linq and string conversion even in Release mode (don't they)?

---稍后添加---

---ADDED LATER---

正如我上面所说的,代码中有很多这样的行,我不是特别想要 #if调试他们全部消灭。

As I said above there are many of these lines in the code and I don't particularly want to #if DEBUG them all out.

我想知道的是如何避免评估Linq和

What I want is to know how to avoid evaluating the Linq and the BitConverter, which I assume will be a performance hit, in Release mode.

推荐答案

您无需执行任何操作!传递给由于 ConditionalAttribute 而被删除的方法的参数的求值已经被抑制。

You do not have to do anything! The evaluation of the parameters passed to a method that is removed because of the ConditionalAttribute is suppressed already.

请参见C#语言中的 条件属性 权威文档规范。引用:

See the section The Conditional attribute in the C# Language Specification for authoritative documentation. Quote:


[...]调用(包括对调用参数
的求值)被省略。

[...] the call (including evaluation of the parameters of the call) is omitted.

要看到该方法实际上包含 ConditionalAttribute ,请参见 Debug.WriteLine(string)文档

To see that the method actually carries the ConditionalAttribute, see Debug.WriteLine(string) documentation.

这篇关于在发布版本(C#)中时,如何停止对调试函数的调用中的参数求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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