如何在 CompilationUnit 中找到方法 Symbol 的 SyntaxNode? [英] How to find the SyntaxNode for a method Symbol in a CompilationUnit?

查看:38
本文介绍了如何在 CompilationUnit 中找到方法 Symbol 的 SyntaxNode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译单元中添加了一堆节点,现在我想查找给定符号对应的语法节点:

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?

我的目标是能够从中获取每个方法的方法体MethodSymbol.

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

推荐答案

使用 ISymbol.DeclaringSyntaxReferences.

这篇关于如何在 CompilationUnit 中找到方法 Symbol 的 SyntaxNode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆