How to GetSemanticModel for any syntax tree in referenced projects of project compilation

五迷三道 提交于 2019-12-08 03:17:28

问题


When I compile a project (using MSBuildWorkspace.Create().OpenProjectAsync().GetCompilationAsync(); I get a Compilation back which includes x SyntaxTrees for the x files within that project. But that project also references other projects. I can see the those projects within the References property of the Compilation, but it seems that calling Compliation.GetSemanticModel does not look through these.

For an arbitrary SyntaxTree that is somewhere within my project (including referenced projects) do I have to manually search through each referenced Compilation of the root project or is there are helper method or some other approach to this?


回答1:


You have to call GetSemanticModel for the compilation that contains that tree. So if project A depends on B, and you've walked over to a Syntax Tree in B, you have to use the compilation for B to ask about B. That's because A doesn't actually "know" about anything in B per se, it's an opaque box.

Your best opportunity is probably to use the workspace API more. When you called OpenProjectAsync(), grab the Solution object from the .Solution property. From there you can walk across all the projects more directly, and if you have a tree you can also do Solution.GetDocument(tree).GetSemanticModelAsync().



来源:https://stackoverflow.com/questions/37339130/how-to-getsemanticmodel-for-any-syntax-tree-in-referenced-projects-of-project-co

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