.NET编译器 - DEBUG与RELEASE [英] .NET Compiler -- DEBUG vs. RELEASE

查看:213
本文介绍了.NET编译器 - DEBUG与RELEASE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,我一直在VB.NET中使用DEBUG编译器常量将消息写入控制台。我也用类似的方式使用System.Diagnostics.Debug.Write。总是我的理解是,当使用RELEASE作为构建选项时,所有这些语句都被编译器忽略,从而释放您的生产代码的debug语句的开销。最近在使用Silverlight 2 Beta 2时,我注意到Visual Studio实际上附加到一个RELEASE构建,我正在运行一个公共网站,并显示DEBUG语句,我假设甚至没有编译!现在,我的第一个倾向是假设我的环境有问题,但我也想问任何人有深入的了解System.Diagnostics.Debug和DEBUG构建选项一般我可能会误会这里。 p>

For years I have been using the DEBUG compiler constant in VB.NET to write messages to the console. I've also been using System.Diagnostics.Debug.Write in similar fashion. It was always my understanding that when RELEASE was used as the build option, that all of these statements were left out by the compiler, freeing your production code of the overhead of debug statements. Recently when working with Silverlight 2 Beta 2, I noticed that Visual Studio actually attached to a RELEASE build that I was running off of a public website and displayed DEBUG statements which I assumed weren't even compiled! Now, my first inclination is to assume that that there is something wrong with my environment, but I also want to ask anyone with deep knowledge on System.Diagnostics.Debug and the DEBUG build option in general what I may be misunderstanding here.

推荐答案

首选方法是实际使用条件属性来包装调试调用,而不是使用编译器指令。 #ifs可能会变得棘手,并可能导致奇怪的构建问题。

The preferred method is to actually use the conditional attribute to wrap your debug calls, not use the compiler directives. #ifs can get tricky and can lead to weird build problems.

使用条件属性的一个例子如下(在C#中,但在VB.NET也工作)

An example of using a conditional attribute is as follows (in C#, but works in VB.NET too):

[ Conditional("Debug") ]
private void WriteDebug(string debugString)
{
  // do stuff
}



当您编译时没有设置DEBUG标志,任何调用WriteDebug将被删除,因为假定发生在Debug.Write()。

When you compile without the DEBUG flag set, any call to WriteDebug will be removed as was assumed was happening with Debug.Write().

这篇关于.NET编译器 - DEBUG与RELEASE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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