问题描述
我这里有这个程序:
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 不起作用.
And somehow, Debug.Assert is not working.
我使用的是 Mono 2.10.5,这是我用来编译和执行的:
I am using Mono 2.10.5 and this is what I use to compile and execute:
dmcs LameProg.cs
mono ./LameProg.exe
我怎样才能做到这一点?我希望它与 C 中的 assert 宏具有相同的效果,也就是说它应该完全使程序崩溃.是否可以使用 Debug.Assert 来做到这一点,或者是否有其他功能可以实现这一点?
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?
推荐答案
Debug.Assert用 [ConditionalAttribute("DEBUG")] 注释.这意味着除非定义了 DEBUG 预处理器符号,否则编译器将删除所有调用.试试这个:
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:
$ dmcs -d:DEBUG LameProg.cs
当断言被命中时,Mono 不会像 Microsoft 的 .NET 实现那样显示对话框.您需要设置一个 TraceListener,例如
$ export MONO_TRACE_LISTENER=Console.Error
$ mono LameProg.exe
<小时>
Debug.Assert 调用通常是在调试版本中使用并从发布版本中删除.如果您想确保某个条件成立,并且此检查应该存在于发布版本中,请使用 if
语句和 throw
异常:
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 and throw
an exception:
public static void Main(string[] args)
{
int a = 2;
int b = 3;
if (a != b)
{
throw new Exception("Bleh");
}
System.Console.WriteLine("Haha it didn't work");
}
这篇关于'Debug.Assert' 语句在 Mono 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!