项目编译引用项目中任意语法树如何GetSemanticModel [英] How to GetSemanticModel for any syntax tree in referenced projects of project compilation

查看:47
本文介绍了项目编译引用项目中任意语法树如何GetSemanticModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译一个项目时(使用 MSBuildWorkspace.Create().OpenProjectAsync().GetCompilationAsync(); 我得到一个 Compilation 返回,其中包括 x 该项目中 x 文件的语法树.但该项目还引用了其他项目.我可以在 Compilation 的 References 属性中看到这些项目,不过好像调用Compliation.GetSemanticModel 并没有看透这些.

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.

对于我的项目(包括引用的项目)中某处的任意SyntaxTree,我是否必须手动搜索每个引用的Compilation根项目还是有帮助方法或其他方法?

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?

推荐答案

您必须为包含该树的编译调用 GetSemanticModel.因此,如果项目 A 依赖于 B,并且您已经走到 B 中的语法树,则必须使用 B 的编译来询问 B.那是因为 A 实际上并不知道"B 中的任何内容 本质上,它是一个不透明的盒子.

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.

您最好的机会可能是更多地使用工作区 API.当您调用 OpenProjectAsync() 时,从 .Solution 属性中获取 Solution 对象.从那里你可以更直接地浏览所有项目,如果你有一棵树,你也可以做 Solution.GetDocument(tree).GetSemanticModelAsync().

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().

这篇关于项目编译引用项目中任意语法树如何GetSemanticModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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