Mono - Debug.Assert does not work

前端 未结 3 1854
后悔当初
后悔当初 2021-02-20 02:37

I have this program here:

namespace TodoPlus {
using System.Diagnostics;

    public class LameProg {
        public LameProg() {}
        public static void Mai         


        
3条回答
  •  伪装坚强ぢ
    2021-02-20 02:56

    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");
    }
    

提交回复
热议问题