问题
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
.
回答1:
Use ISymbol.DeclaringSyntaxReferences
.
来源:https://stackoverflow.com/questions/11428662/how-to-find-the-syntaxnode-for-a-method-symbol-in-a-compilationunit