单 - Debug.Assert的不工作 [英] Mono - Debug.Assert does not work

查看:89
本文介绍了单 - Debug.Assert的不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有这个程序:

 命名空间TodoPlus {$ B $使用System.Diagnostics程序B: 

公共类LameProg {
公共LameProg(){}
公共静态无效的主要(字串[] args){
int类型的= 2;
INT B = 3;
Debug.Assert的(一个== B的Bleh);
的System.Console.WriteLine(哈哈它不工作);
}
}
}

和不知何故,Debug.Assert的。不工作



我使用单声道2.10.5,这就是我使用的编译和执行:



$契LameProg.cs



$单声道./LameProg.exe



我怎样才能使这项工作?我希望它有在C断言宏,这就是说它应该只是彻头彻尾的程序崩溃的相同的效果。是否有可能与Debug.Assert的做到这一点还是有实现这种其他一些功能?



感谢。


解决方案

  1. Debug.Assert的都标注有 [ConditionalAttribute(DEBUG )。这意味着所有的调用由编译器删除,除非DEBUG预处理符号定义。试试这个:



    <预类=郎无prettyprint-覆盖> $契-d:DEBUG LameProg.cs


  2. 单并不显示像微软的.NET实现一个对话框时断言被击中。您需要设置一个的TraceListener ,如:



    <预类=郎无prettyprint-覆盖> $出口MONO_TRACE_LISTENER = Console.Error
    $单LameProg.exe







Debug.Assert的调用通常用于调试版本,并删除了发布版本。如果你想确保一定的条件成立,而这种检查应该出现在发布版本,使用如果语句和异常:



<预类=郎-CS prettyprint-覆盖> 公共静态无效的主要(字串[] args)
{
int类型的= 2;
INT B = 3;
如果(A = B!)
{
抛出新的异常(的Bleh);
}
的System.Console.WriteLine(哈哈它不工作);
}


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 affect 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?

Thanks.

解决方案

  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
    

  2. 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.

    $ export MONO_TRACE_LISTENER=Console.Error
    $ mono LameProg.exe
    


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的不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆