The #39;Debug.Assert#39; statement does not work in Mono(Debug.Assert 语句在 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 不起作用.
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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'Debug.Assert' 语句在 Mono 中不起作用


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01