Debug.Assert似乎不适用于Mono

考虑以下C#程序:

using System; using System.Diagnostics; namespace Test { class MainClass { public static void Main (string[] args) { Debug.Assert(false); Debug.Fail("fail!"); Console.WriteLine ("Hello World!"); } } } 

使用以下方法编译时:

 dmcs -debug -d:DEBUG Main.cs 

然后运行它:

 mono --debug Main.exe 

断言和失败似乎被忽略了。 输出只是:

 Hello World! 

我在StackOverflow上检查了其他相关问题,但我找不到解决方案。 特别是解决方案在Mono中给出- Debug.Assert不起作用不起作用。 (更新:更新的解决方案确实有效,请参阅下面的评论。)

我在Ubuntu 11.10上使用Mono 2.10.5-1。

C#on mono – http://ebsteblog.wordpress.com/2009/05/06/debugassert-and-mono/

摘自文章:

…如果为应用创建.config文件并将assertuienabled属性设置为true,则会得到与.NET相同的对话框…文件app.config:

       

旧答案:C ++注释如果未在命令行/编译选项上指定-define DEBUG。

用于调试添加

 #define DEBUG 

在代码的开头或

 #define TRACE 

追踪。

请参阅此处的解决方案: http : //lists.ximian.com/pipermail/mono-list/2006-December/033774.html

ps:我用C ++而不是C#试过这个。 这可能不适用于C#。

您可以使用xml配置,也可以通过在运行时添加跟踪侦听器将其置于程序的控制之下:

 var tl = new System.Diagnostics.ConsoleTraceListener(); System.Diagnostics.Debug.Listeners.Add ( tl ); 

这具有额外的优点,您可以在程序启动后启用它。