简体   繁体   中英

Conditional breakpoint not working in Visual Studio 2015

I have an issue debugging my C# code in Visual Studio 2015.

I want to add a simple expression into a breakpoint,

So I added hierarchyRelation != null as condition. 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.

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. Is there any issue ? The closest related question I found was this , but it was in 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

在此输入图像描述

The issue is that the hierarchyRelation is a dynamic variable although I'm not totally sure why. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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