简体   繁体   English

Debug.Assert是否在发布模式下生成IL?

[英]Does Debug.Assert generate IL in release mode?

When Debug.Assert() method calls exist in source code and I compile in release mode, does the compiler generate the IL for the Debug.Assert() even though it's not called? 当源代码中存在Debug.Assert()方法调用并且我在发布模式下编译时,编译器是否为Debug.Assert()生成IL,即使它没有被调用?

One of our developers added an Assert recently that displays information about our internal security. 我们的一位开发人员最近添加了一个Assert,显示有关内部安全性的信息。 Could someone look at the release mode IL and figure out the text for the assert? 有人可以查看发布模式IL并找出断言的文本吗?

It does not by default, unless you define the DEBUG symbol (and by default, for Release that is turned off). 除非您定义DEBUG符号(默认情况下,对于已关闭的Release),否则默认情况下不会

To verify, open your Project Properties and select the Build pane in Visual Studio. 要进行验证,请打开“项目属性”,然后在Visual Studio中选择“构建”窗格。 It will show the checkbox "Define DEBUG constant". 它将显示“Define DEBUG constant”复选框。 If it is turned on for Release, then asserts will fire; 如果为Release打开它,则会触发断言; otherwise, they won't. 否则,他们不会。

No, the members of the Debug class (with the ConditionalAttribute attribute) do not emit IL. 不, Debug类的成员(带有ConditionalAttribute属性)不会发出IL。 There is no explicit mention on MSDN , however the following two quotes imply the behaviour quite well, so to augment Roy's answer: 在MSDN上没有明确提及 ,但是以下两个引用意味着行为相当好,所以为了增加Roy的答案:

If you use methods in the Debug class to print debugging information and check your logic with assertions, you can make your code more robust without affecting the performance and code size of your shipping product. 如果使用Debug类中的方法来打印调试信息并使用断言检查逻辑,则可以使代码更加健壮,而不会影响传送产品的性能和代码大小。

So, no size difference implies no output from these whatsoever, and 因此,没有任何尺寸差异意味着没有任何输出,并且

The ConditionalAttribute attribute is applied to the methods of Debug. ConditionalAttribute属性应用于Debug的方法。 Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. 除非将“DEBUG”定义为条件编译符号,否则支持ConditionalAttribute的编译器将忽略对这些方法的调用。 Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol. 请参阅编译器的文档以确定是否支持ConditionalAttribute以及定义条件编译符号的语法。

Which means that, at the compiler level, these calls won't even be considered (when DEBUG is not defined.) 这意味着,在编译器级别,甚至不会考虑这些调用(当未定义DEBUG时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM