简体   繁体   English

条件断点在Visual Studio 2015中不起作用

[英]Conditional breakpoint not working in Visual Studio 2015

I have an issue debugging my C# code in Visual Studio 2015. 我在Visual Studio 2015中调试我的C#代码时遇到问题。

I want to add a simple expression into a breakpoint, 我想在断点中添加一个简单的表达式,

So I added hierarchyRelation != null as condition. 所以我添加了hierarchyRelation != null作为条件。 That is a local variable of the method I am debugging, and it exists. 这是我正在调试的方法的局部变量,它存在。

However, in runtime I get the following error 但是,在运行时我收到以下错误

"The condition for a breakpoint failed to execute. The condition was "hierarchyRelation != null". The error returned was "The breakpoint condition must evaluate to a boolean operation". Click OK to stop at this breakpoint. “断点的条件无法执行。条件是”hierarchyRelation!= null“。返回的错误是”断点条件必须求值为布尔运算“。单击”确定“以停止此断点。

Actually, the condition was more complex, but this is the simplest case that reproduces the problem. 实际上,条件更复杂,但这是重现问题的最简单的情况。 I tried variants, and even comparing properties of this variable and it always fails the same. 我尝试了变体,甚至比较了这个变量的属性,但总是失败了。

If I try a constant condition, like 1 != 2 or 1 = 1 it works fine. 如果我尝试一个恒定的条件,如1 != 21 = 1它可以正常工作。 Is there any issue ? 有什么问题吗? The closest related question I found was this , but it was in vb code . 我找到的最接近的相关问题是这个 ,但它是在vb code Its solution was to add a debug method directly in the code. 它的解决方案是直接在代码中添加调试方法。 Although I can do that, I want to know why is this not working. 虽然我可以这样做,但我想知道为什么这不起作用。

The method code 方法代码

private HierarchyNodeDto GetNodeTreeThatContainsText<TRollup, TLeaf, THierarchyRelation>(HierarchyNodeDto root, string text, PreFilter preFilter, Func<TLeaf, bool> leafContainsTextFunc, bool parentContainsText) where TRollup: HierarchyNodeDto where TLeaf: HierarchyNodeDto {
            dynamic rootNode = root as TRollup;
            if (rootNode != null) {
                if (rootNode.Nodes == null) {
                    return null;
                }
                var childNodesWithText = new List<THierarchyRelation>();
                foreach (var hierarchyRelation in rootNode.Nodes) {
                    var isLeaf = hierarchyRelation.Node.GetType() == typeof(TransactionTypeHierarchyLeafDto) || hierarchyRelation.Node.GetType() == typeof(AccountHierarchyLeafDto);
                    if (!isLeaf && hierarchyRelation.Node.Name != null && hierarchyRelation.Node.Name.ToLower().Contains(text) && preFilter != PreFilter.Leafs) {
                        childNodesWithText.Add(hierarchyRelation);
                        continue;
                    }
                    var subtreeThatContainsText = this.GetNodeTreeThatContainsText<TRollup, TLeaf, THierarchyRelation>(hierarchyRelation.Node, text, preFilter, leafContainsTextFunc, rootNode.Name.ToLower().Contains(text));
                    if (subtreeThatContainsText == null) {
                        continue;
                    }
                    hierarchyRelation.Node = subtreeThatContainsText;
                    childNodesWithText.Add(hierarchyRelation);
                }
                rootNode.Nodes = childNodesWithText;
                if (rootNode.Nodes.Count > 0 || (rootNode.Name.ToLower().Contains(text) && preFilter != PreFilter.Leafs)) {
                    return rootNode;
                }
                return null;
            }
            var rootLeaf = root as TLeaf;

            return rootLeaf != null && ((leafContainsTextFunc.Invoke(rootLeaf) && preFilter != PreFilter.Nodes) || (parentContainsText && preFilter != PreFilter.Leafs)) ? rootLeaf : null;
        }

I am adding the breakpoint in the first line inside the foreach 我在foreach中的第一行添加断点

在此输入图像描述

The issue is that the hierarchyRelation is a dynamic variable although I'm not totally sure why. 问题是hierarchyRelation是一个动态变量,虽然我不完全确定原因。 According to Expressions in the Debugger it should work (I couldn't find a reason why it shouldn't) . 根据调试器中的表达式它应该工作(我找不到它不应该的原因)。

    static void Main(string[] args)
    {
        dynamic foo = new Foo();

        // conditional breakpoint 'foo.Nodes == null' here
    }

    internal class Foo
    {
        public IEnumerable<Foo> Nodes = null;
    }

This code triggers the same exception whenever the debugger passes and evaluates the conditional breakpoint. 只要调试器通过并评估条件断点,此代码就会触发相同的异常。 Statically typing the foo variable will make the debugger able to evaluate the expression and break whenever needed. 静态键入foo变量将使调试器能够在需要时评估表达式并中断。

A bit late to the party but anyway, I hit the same problem like this as well, with a dynamic object too. 派对有点晚了但无论如何,我也遇到了同样的问题,也有动态对象。

What I did to solve this was to cast it as an object in the breakpoint's conditional expression itself; 我所做的就是将它作为一个对象放在断点的条件表达式中; so in your case, doing the following would work: 所以在你的情况下,做以下工作会起作用:

(object)hierarchyRelation!=null

Give it a go and see if that works out for you. 试一试,看看是否适合你。

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

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