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?