我需要从给定的 SymbolAnalysisContext
访问语法树(完整的源文件文本)。我发现我可以调用 context.Compilation.SyntaxTrees
但从中选择哪一个?还有别的办法吗?
最佳答案
一个符号可以在一个或多个语法树中定义,或者在元数据中定义(通过引用)。您可以通过 ISymbol.Locations 获得所有这些,其中:
Gets the locations where the symbol was originally defined, either in source or metadata. Some symbols (for example, partial classes) may be defined in more than one location.
如果您只对源代码中定义的那些感兴趣,您可以这样做:
var syntaxTrees = from x in context.Symbol.Locations
where x.IsInSource
select x.SourceTree;
关于c# - 从 SymbolAnalysisContext 获取语法树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37695086/