user541686
user541686

Reputation: 210352

How to find the SyntaxNode for a method Symbol in a CompilationUnit?

I've added a bunch of nodes to a compilation unit, and now I would like to look up the syntax node corresponding to a given symbol:

var compilation = Compilation.Create("HelloWorld")
    .AddSyntaxTrees(SyntaxTree.ParseCompilationUnit("<some namespace>"));

ISymbol symbol =  // some arbitrary symbol, e.g. a method whose syntax node I had
    compilation.GlobalNamespace.GetNamespaceMembers().First();

SyntaxToken token = ???;   // how do I get the token for that symbol?

How do I get the token for that symbol?

Note:

My goal is to be able to get the method body for each method from it MethodSymbol.

Upvotes: 18

Views: 6022

Answers (1)

Kevin Pilch
Kevin Pilch

Reputation: 11605

Use ISymbol.DeclaringSyntaxReferences.

Upvotes: 28

Related Questions