“调试.断言”语句在 Mono 中不起作用
我这里有这个程序:
namespace TodoPlus {
using System.Diagnostics;
public class LameProg {
public LameProg() {}
public static void Main(string[] args) {
int a = 2;
int b = 3;
Debug.Assert(a == b, "Bleh");
System.Console.WriteLine("Haha, it didn't work");
}
}
}
不知何故,Debug.Assert 不起作用。
我正在使用 Mono 2.10.5,这就是我用来编译和执行的:
dmcs LameProg.cs
mono ./LameProg.exe
How can I make this work?我希望它能与 C 中的断言宏具有相同的效果,也就是说它应该让程序彻底崩溃。是否可以使用 Debug.Assert 来完成此操作,或者是否有其他函数可以实现此目的?
I have this program here:
namespace TodoPlus {
using System.Diagnostics;
public class LameProg {
public LameProg() {}
public static void Main(string[] args) {
int a = 2;
int b = 3;
Debug.Assert(a == b, "Bleh");
System.Console.WriteLine("Haha, it didn't work");
}
}
}
And somehow, Debug.Assert is not working.
I am using Mono 2.10.5 and this is what I use to compile and execute:
dmcs LameProg.cs
mono ./LameProg.exe
How can I make this work? I wish it to have the same effect as the assert macro in C, which is to say it should just downright crash the program. Is it possible to do this with Debug.Assert or is there some other function that achieves this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Debug.Assert 注释为[ConditionalAttribute("DEBUG")]。这意味着除非定义了 DEBUG 预处理器符号,否则所有调用都会被编译器删除。试试这个:
当断言被命中时,Mono 不会像 Microsoft 的 .NET 实现那样显示对话框。您需要设置一个 TraceListener,例如
Debug.Assert 调用是通常在调试版本中使用并从发布版本中删除。如果您想确保某个条件成立,并且此检查应该存在于发布版本中,请使用
if
语句并抛出
异常:Debug.Assert is annotated with [ConditionalAttribute("DEBUG")]. This means that all invocations are removed by the compiler unless the DEBUG preprocessor symbol is defined. Try this:
Mono does not show a dialog box like Microsoft's .NET implementation when an assertion is hit. You need to set a TraceListener, e.g.
Debug.Assert invocations are typically used in debug builds and removed from release builds. If you want to make sure that a certain condition holds, and this check should be present in release builds, use an
if
statement andthrow
an exception:还有另一个技巧:您可以通过 TraceListener 添加“立即退出”行为,因为 Debug.Assert 失败会触发跟踪侦听器中对 Fail() 的调用。
您仍然需要 -define:DEBUG (和 TRACE?)。我个人期望 Assert() 调用(在 DEBUG 版本中)停止我的程序,转储调试信息并退出。所以,我是这样做的:
在我的代码中,我安装了一个自定义跟踪侦听器来转储堆栈并添加对 Exit() 的调用。还有中提琴!您有对 Assert.Fail() 的行业标准响应。例如,您还可以在此处打印时间戳等。
结合调用:
您就可以开始了。
There's another trick: you can add "exit now" behavior through a TraceListener, because Debug.Assert failures trigger a call to Fail() in the trace listener.
You still need to -define:DEBUG (and TRACE?). I personally expect Assert() calls (in DEBUG builds) to stop my program, dump debug info and exit. So, this is how I do it:
In my code I install a custom trace listener to dump stack and add a call to Exit(). And viola! You have an industry standard response to Assert.Fail(). You could also, for example, print timestamps here, etc.
Combine with a call to:
And you're good to go.
我相信您需要两件事:编译器的 DEBUG 属性和运行时的“跟踪侦听器”。我让它工作起来
仍然不会像我预期的那样在断言失败时立即退出,但至少它会打印一些东西。
I believe you need two things: the DEBUG attribute to the compiler, and a 'trace listener' for the runtime. I got it to work with
That still doesn't exit immediately on assertion failure as I would have expected, but at least it prints something.