0

I am trying to use Roslyn and implement my own node visitors by extending CSharpSyntaxWalker and overriding its Visit... methods. But I have a problem.

Consider the following C# class:

class MyClass {
  private void DoSomething() { ... }
  public void DoSomethingElse() { ... }
  public class MyClass2 {
    private int GetSomething() { ... }
  }
}

I need to react only to methods defined in MyClass. I can get a reference to that node. So I can get a reference to a SyntaxNode representing MyClass, so that I can do:

CSharpSyntaxWalker.Visit(node); // node is a SyntaxNode

However the visit will react to GetSomething method, which I want to exclude from the visit. Actually I want to exclude MyClass2. So, in my custom walker, I can override visit methods to react to methods, but when I encounter a class declaration i would like to instruct the visitor to ignore that node.

public class MyWalker : CSharpSyntaxWalker {
  public override void VisitMethodDeclaration { ... }
  public override void VisitClassDeclaration {
    // Do something to exclude this node and its subtree from the visit...
  }
}

How can I do that?

Andry
  • 16,172
  • 27
  • 138
  • 246

1 Answers1

4

Just don't call base.Visit for nodes you don't want to recurse into.

Kevin Pilch
  • 11,485
  • 1
  • 39
  • 41