简体   繁体   English

Debug.Assert并不总是在C#中工作

[英]Debug.Assert not always working in c#

I have a strange problem with Asserts being ignored. 我有一个奇怪的问题,即断言被忽略。 You can reproduce it with this minimal example. 您可以通过以下最小示例来重现它。 I am wondering why this problem occurs and how to face it: 我想知道为什么会出现此问题以及如何解决它:

public class TestAssert
{
    public string EmptyString
    {
        get
        {
            System.Diagnostics.Debug.Assert(false);
            return string.Empty;
        }
    }
    Dictionary<string, object> dict = new Dictionary<string, object>();

    public void ShowAssertIgnored()
    {
        var foo = dict[EmptyString];
    }
}    

You can see that the Debug.Assert(false) is ignored even though the property is evaluated. 您可以看到即使评估了该属性,也会忽略Debug.Assert(false) Just call 刚打电话

var test = new TestAssert();
test.ShowAssertIgnored();

and you should see what I mean (also shown on the image). 并且您应该明白我的意思(也显示在图片上)。 Debug.Assert不评估,但属性为

The code is compiled and run in Debug (other asserts work fine!), 32 bit, x86 + AnyCPU, VS2012 professional, .Net Framework 4 该代码在Debug (其他断言可以正常工作!),32位,x86 + AnyCPU,VS2012 Professional,.Net Framework 4中编译并运行。

Edit: The project is a console application and I was running it for several times in a row. 编辑:该项目是一个控制台应用程序,我已经连续运行了好几次。 Having a breakpoint before System.Diagnostics.Debug.Assert(false); System.Diagnostics.Debug.Assert(false);之前有一个断点System.Diagnostics.Debug.Assert(false); most often a messagebox appears. 通常会出现一个消息框。 But not always: When I just retry the same situation several times I sometimes see the result in the console. 但并非总是如此:当我多次尝试相同的情况时,有时会在控制台中看到结果。

To say it again: I can reproduce non-deterministic behaviour in the VS2012 debugger! 再说一遍:我可以在VS2012调试器中重现非确定性行为!

If you read the docs , you'll see that Debug.Assert isn't designed to throw. 如果您阅读了这些文档 ,将会看到Debug.Assert不是要抛出的。

Typically, the Assert(Boolean) method is used to identify logic errors during program development. 通常,Assert(Boolean)方法用于在程序开发过程中识别逻辑错误。 Assert evaluates the condition. 断言评估条件。 If the result is false, it sends a failure message to the Listeners collection. 如果结果为假,则它将失败消息发送到Listeners集合。 You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection. 您可以通过向Listeners集合添加TraceListener或从其中删除一个TraceListener来自定义此行为。

When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. 当应用程序在用户界面模式下运行时,它将显示一个消息框,其中显示了带有文件和行号的调用堆栈。 The message box contains three buttons: Abort, Retry, and Ignore. 该消息框包含三个按钮:中止,重试和忽略。 Clicking the Abort button terminates the application. 单击“中止”按钮将终止应用程序。 Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. 如果您的应用程序在调试器中运行,则单击“重试”会将您转到调试器中的代码,如果您的应用程序未在调试器中运行,则单击它可以打开。 Clicking Ignore continues with the next instruction in the code. 单击忽略继续执行代码中的下一条指令。

If running in an UI environment, it may trigger throw-like behaviour that is defined in the listeners that the UI environment/test-runner sets up. 如果在UI环境中运行,它可能会触发在UI环境/测试运行器所设置的侦听器中定义的类抛出行为。

So, in short, Debug.Assert(false) won't halt your application but its listeners might. 因此,简而言之, Debug.Assert(false)不会暂停您的应用程序,但它的侦听器可能会暂停。

Assuming that there is no UI here... If you want your code to fail, then you'll need to write your own TraceListener: 假设这里没有UI ...如果您希望代码失败,那么您需要编写自己的TraceListener:

public class MyTraceListener : TraceListener
{
    public override void Write(string msg)
    {
        throw new Exception(msg);
    }
    public override void WriteLine(string msg)
    {
        throw new Exception(msg);
    }
}

and add it to the listeners collection: 并将其添加到listeners集合中:

Debug.Listeners.Add(new MyTraceListener());

That's an expected behaviour , Debug.Assert() works in debug build only : 这是预期的行为Debug.Assert() 调试版本中起作用:

By default, the Debug.Assert method works only in debug builds. 默认情况下,Debug.Assert方法仅在调试版本中起作用。 Use the Trace.Assert method if you want to do assertions in release builds. 如果要在发行版本中进行断言,请使用Trace.Assert方法。 For more information, see Assertions in Managed Code. 有关更多信息,请参见托管代码中的断言。

https://msdn.microsoft.com/en-us/library/kssw4w7z(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/kssw4w7z(v=vs.110).aspx

use Trace.Assert() if you want it working in release as well. 如果希望它也可以在发行版中使用,请使用Trace.Assert()

Have you checked your project settings to define the DEBUG constant? 您是否检查过项目设置以定义DEBUG常量? The Debug.Assert() methods have the [ConditionalAttribute("DEBUG")] attribute, which means, they will by ignored during compilation, if the "DEBUG" constant is not defined. Debug.Assert()方法具有[ConditionalAttribute(“ DEBUG”)]属性,这意味着,如果未定义“ DEBUG”常量,则在编译期间将忽略它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM