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

扶醉桌前 提交于 2020-01-03 07:28:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!