Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

roslyn

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.

like image 317
user541686 Avatar asked Jul 11 '12 08:07

user541686


1 Answers

Use ISymbol.DeclaringSyntaxReferences.

like image 79
Kevin Pilch Avatar answered Oct 21 '22 09:10

Kevin Pilch