bit*_*onk 2 .net debugging clr assertions
如果我在visual studio中为我的C#项目定义了Debug常量,我可以确定将评估断言并在失败时显示消息框.但是什么标志属性使CLR在运行时实际上决定是否评估和显示断言.在定义DEBUG时,断言代码是否不会在IL中结束?或者它是程序集的DebuggableAttribute中的DebuggableAttribute.DebuggingModes标志的关键点?如果是这样,它的枚举值必须存在?这是如何工作的?
如果在没有定义DEBUG预处理程序符号的情况下进行编译,则将从编译的代码中省略对Debug.Assert的任何调用.
如果你查看Debug.Assert的文档,你会看到它[ConditionalAttribute("DEBUG")]在声明上.ConditionalAttribute用于决定在编译时是否实际发出方法调用.
如果条件属性表示未进行调用,则也会省略任何参数评估.这是一个例子:
using System;
using System.Diagnostics;
class Test
{
static void Main()
{
Foo(Bar());
}
[Conditional("TEST")]
static void Foo(string x)
{
Console.WriteLine("Foo called");
}
static string Bar()
{
Console.WriteLine("Bar called");
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
定义TEST时,会调用这两种方法:
c:\Users\Jon> csc Test.cs /d:TEST
c:\Users\Jon> test.exe
Bar called
Foo called
Run Code Online (Sandbox Code Playgroud)
如果未定义TEST,则不会调用它们:
c:\Users\Jon> csc Test.cs /d:TEST
c:\Users\Jon> test.exe
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
248 次 |
| 最近记录: |