[有条件的(“DEBUG”)]或#if DEBUG [英] [Conditional("DEBUG")] or #if DEBUG

查看:130
本文介绍了[有条件的(“DEBUG”)]或#if DEBUG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读C#文档之后,条件属性似乎是通过

去的方式,但在检查IL后,似乎那些方法仍然存在并且我想要CLR删除他们。使用#if DEBUG意味着在发布模式下编译时代码甚至不会达到IL。


使用条件属性有什么好处?我是否正确

认为使用[条件(DEBUG)]

超过#if DEBUG会有很小的性能开销。

After reading C# documentation the Conditional attribute seemed the way to
go, but after inspecting the IL it seems those methods are still there and I
imagine the CLR removes them. Using #if DEBUG means the code does not even
reach the IL when compiling in release mode.

Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using [Conditional("DEBUG")]
over #if DEBUG.

推荐答案

John,

对被调用方法使用[条件(DEBUG)]没有任何好处

本身本身的好处是调用方法。您可以将所有

调用留给[条件(DEBUG)]方法,编译器将悄悄地将b b $ b更改为发布版本中的无操作,或者实际方法调用

调试版本。


我要做的是同时使用[条件(DEBUG)]和& #if DEBUG on internal

方法,这将删除电话&来自

程序集的该方法的代码,对于类库中的公共方法,我将仅使用

[条件(DEBUG)]作为针对该类编译的程序集库可能是
或者可能没有定义DEBUG。


有关使用[条件(DEBUG)]的示例,请参阅所有方法在

System.Diagnostics.Debug& System.Diagnostics.Trace类。 :-)


有关详细信息,请参阅:
http://msdn.microsoft.com/library/de...TraceDebug.asp

http:// msdn。 microsoft.com/msdnmag/is...r/default.aspx

希望这有帮助

Jay

" John Smith" <所以***** @ microsoft.com>在消息中写道

news:5y ********************* @ twister.southeast.rr。 com ...
John,
There is no benefit of using [Conditional("DEBUG")] to the called method
itself per se, the benefit is for the calling methods. You can leave all the
calls to a [Conditional("DEBUG")] method and the compiler will quietly
change them into no ops on release builds, or the actual method calls in
debug builds.

What I will do is use both [Conditional("DEBUG")] & #if DEBUG on internal
methods, which will remove the calls & the code for that method from the
assembly, for public methods in class libraries I will use only
[Conditional("DEBUG")] as assemblies compiled against the class library may
or may not have defined DEBUG.

For an example of the use of [Conditional("DEBUG")], see all the methods in
the System.Diagnostics.Debug & System.Diagnostics.Trace classes. :-)

For more details see:
http://msdn.microsoft.com/library/de...TraceDebug.asp

http://msdn.microsoft.com/msdnmag/is...r/default.aspx

Hope this helps
Jay

"John Smith" <so*****@microsoft.com> wrote in message
news:5y*********************@twister.southeast.rr. com...
阅读C#文档之后,条件属性似乎是
去的方式,但在检查IL后,似乎那些方法仍然存在并且
我想象CLR删除它们。使用#if DEBUG意味着在发布模式下编译时代码甚至不会到达IL。

使用条件属性有什么好处?我是否正确认为使用
[条件(DEBUG)]而不是#if DEBUG会有很小的性能开销。
After reading C# documentation the Conditional attribute seemed the way to
go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG means the code does not even
reach the IL when compiling in release mode.

Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using [Conditional("DEBUG")] over #if DEBUG.



感谢您提供简明扼要的答案。公共类库似乎是条件属性的原因。

只是为了澄清当你说编译器悄悄地将它们变成nops时你

表示JIT编译器,而不是编译器而不是将C#转换为IL。
Thank you for the clear and concise answer. Public class libraries seem the
reason d''etre for the Conditional Attribute.

Just to clarify when you say the compiler quietly turns them into nops you
mean the JIT compiler, not the compiler than translates C# into IL.


我会说还有另一个好理由使用条件属性:

调用调试方法的代码更容易阅读。


使用传统方法,你不能只是#ifdef调试

方法,因为对该方法的调用将无法编译,所以

你必须用#ifdef'来包围它们,这会引入一个

代码中的混乱很多。


使用Conditional属性,你可以留下所有的电话,

和编译器(不知道哪一个?)会默默地删除它们

发布构建时不会损失运行时速度。

-
http://www.kynosarges.de
I''d say there''s another good reason to use the Conditional attribute:
the code that calls the debug method is much easier to read.

With the traditional approach, you can''t just #ifdef out the debug
method because the calls to that method will then fail to compile, so
you have to surround them with #ifdef''s as well which introduces a
great deal of clutter in the code.

With the Conditional attribute, you can just leave in all the calls,
and the compiler (don''t know which one?) will silently remove them in
Release builds at no loss of runtime speed.
--
http://www.kynosarges.de

这篇关于[有条件的(“DEBUG”)]或#if DEBUG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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