'Debug.Assert' 语句在 Mono 中不起作用

The #39;Debug.Assert#39; statement does not work in Mono(Debug.Assert 语句在 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?

推荐答案

  1. Debug.Assert用 [ConditionalAttribute("DEBUG")] 注释.这意味着除非定义了 DEBUG 预处理器符号,否则编译器将删除所有调用.试试这个:

  1. 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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
    Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
    Programmatically show the desktop(以编程方式显示桌面)
    c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
    InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
    LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)