“调试.断言”语句在 Mono 中不起作用

发布于 2024-12-05 10:35:55 字数 656 浏览 0 评论 0原文

我这里有这个程序:

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 不起作用。

我正在使用 Mono 2.10.5,这就是我用来编译和执行的:

dmcs LameProg.cs

mono ./LameProg.exe

How can I make this work?我希望它能与 C 中的断言宏具有相同的效果,也就是说它应该让程序彻底崩溃。是否可以使用 Debug.Assert 来完成此操作,或者是否有其他函数可以实现此目的?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请远离我 2024-12-12 10:35:55
  1. Debug.Assert 注释为[ConditionalAttribute("DEBUG")]。这意味着除非定义了 DEBUG 预处理器符号,否则所有调用都会被编译器删除。试试这个:

    $ dmcs -d:DEBUG LameProg.cs
    
  2. 当断言被命中时,Mono 不会像 Microsoft 的 .NET 实现那样显示对话框。您需要设置一个 TraceListener,例如

    $ export MONO_TRACE_LISTENER=Console.Error
    $ 单声道 LameProg.exe
    

Debug.Assert 调用是通常在调试版本中使用并从发布版本中删除。如果您想确保某个条件成立,并且此检查应该存在于发布版本中,请使用 if 语句并抛出 异常:

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");
}
  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");
}
他夏了夏天 2024-12-12 10:35:55

还有另一个技巧:您可以通过 TraceListener 添加“立即退出”行为,因为 Debug.Assert 失败会触发跟踪侦听器中对 Fail() 的调用。

您仍然需要 -define:DEBUG (和 TRACE?)。我个人期望 Assert() 调用(在 DEBUG 版本中)停止我的程序,转储调试信息并退出。所以,我是这样做的:

在我的代码中,我安装了一个自定义跟踪侦听器来转储堆栈并添加对 Exit() 的调用。还有中提琴!您有对 Assert.Fail() 的行业标准响应。例如,您还可以在此处打印时间戳等。

public class DumpStackTraceListener : TraceListener
{
  public override void Write( string message )
  {
     Console.Write( message );
  }

  public override void WriteLine(string message)
  {
     Console.WriteLine( message );
  }

  public override void Fail(string message)
  {
     Fail( message, String.Empty );
  }

  public override void Fail(string message1, string message2)
  {
     if (null == message2)
        message2 = String.Empty;

     Console.WriteLine( "{0}: {1}", message1, message2 );
     Console.WriteLine( "Stack Trace:" );

     StackTrace trace = new StackTrace( true );
     foreach (StackFrame frame in trace.GetFrames())
     {
        MethodBase frameClass = frame.GetMethod();
        Console.WriteLine( "  {2}.{3} {0}:{1}", 
                           frame.GetFileName(),
                           frame.GetFileLineNumber(),
                           frameClass.DeclaringType,
                           frameClass.Name );
     }

#if DEBUG
     Console.WriteLine( "Exiting because Fail" );
     Environment.Exit( 1 );
#endif
  }
}

结合调用:

#if DEBUG
   Debug.Listeners.Add( new DumpStackTraceListener() );
#endif

您就可以开始了。

There's another trick: you can add "exit now" behavior through a TraceListener, because Debug.Assert failures trigger a call to Fail() in the trace listener.

You still need to -define:DEBUG (and TRACE?). I personally expect Assert() calls (in DEBUG builds) to stop my program, dump debug info and exit. So, this is how I do it:

In my code I install a custom trace listener to dump stack and add a call to Exit(). And viola! You have an industry standard response to Assert.Fail(). You could also, for example, print timestamps here, etc.

public class DumpStackTraceListener : TraceListener
{
  public override void Write( string message )
  {
     Console.Write( message );
  }

  public override void WriteLine(string message)
  {
     Console.WriteLine( message );
  }

  public override void Fail(string message)
  {
     Fail( message, String.Empty );
  }

  public override void Fail(string message1, string message2)
  {
     if (null == message2)
        message2 = String.Empty;

     Console.WriteLine( "{0}: {1}", message1, message2 );
     Console.WriteLine( "Stack Trace:" );

     StackTrace trace = new StackTrace( true );
     foreach (StackFrame frame in trace.GetFrames())
     {
        MethodBase frameClass = frame.GetMethod();
        Console.WriteLine( "  {2}.{3} {0}:{1}", 
                           frame.GetFileName(),
                           frame.GetFileLineNumber(),
                           frameClass.DeclaringType,
                           frameClass.Name );
     }

#if DEBUG
     Console.WriteLine( "Exiting because Fail" );
     Environment.Exit( 1 );
#endif
  }
}

Combine with a call to:

#if DEBUG
   Debug.Listeners.Add( new DumpStackTraceListener() );
#endif

And you're good to go.

寄居人 2024-12-12 10:35:55

我相信您需要两件事:编译器的 DEBUG 属性和运行时的“跟踪侦听器”。我让它工作起来

% export MONO_TRACE_LISTENER=Console.Error
% mcs -define:DEBUG -debug Prog.cs
% mono Prog.exe

仍然不会像我预期的那样在断言失败时立即退出,但至少它会打印一些东西。

I believe you need two things: the DEBUG attribute to the compiler, and a 'trace listener' for the runtime. I got it to work with

% export MONO_TRACE_LISTENER=Console.Error
% mcs -define:DEBUG -debug Prog.cs
% mono Prog.exe

That still doesn't exit immediately on assertion failure as I would have expected, but at least it prints something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文