Roslyn 获取类的依赖项 [英] Roslyn getting dependencies for class

查看:39
本文介绍了Roslyn 获取类的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定一个类,它引用了什么(其他命名空间或外部库命名空间).对于给定的文档/语法树等,似乎是开箱即用的,没有办法做到这一点......还有更多我需要的只是使用符号查找器,遍历整个代码库中的每个文件,然后调用查找,粘贴找到的引用在地图中,然后向后导航地图.

I'm trying to determine for a class, what does it reference (other namespaces, or external library namespaces). Seems like out of box for a given document/syntaxtree etc, theres no way to do that... and more that I need to just Use symbol finder, iterate through every file in the entire code base, and call find, sticking found references in a map, and then navigating the map backwards.

我错了吗?我错过了一些简单的东西吗?我只是想建立一个依赖关系图......如果我从这个类开始,这里是它最终需要递归的所有东西.我不介意做研究,但我觉得我错过了一些东西......任何线索都会有所帮助

Am I wrong here? Am I missing something simple? I'm just trying to build a dependency graph... If I start with this class, heres everything that this ultimately needs recursively. I don't mind doing research, but I feel like I'm missing something... and any lead would be helpful

简要伪代码概念:

var msWorkspace = MSBuildWorkspace.Create();
var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;
foreach (var project in solution.Projects)
{
    Append(project.Name);
    foreach (var document in project.Documents)
    {
        Append("\t\t\t" + document.Name);
        if (document.SupportsSemanticModel)
        {
            SemanticModel model = await document.GetSemanticModelAsync();
            var node = await document.GetSyntaxRootAsync();
            //Need to be able to gather dependencies for this current doc 
        }
     }
}

推荐答案

您可以使用语义模型轻松地做到这一点:

You can do that fairly easily using the semantic model:

node.Descendents()
    .SelectMany(n => semanticModel.GetSymbols(n, workspace, true, new CancellationToken())
    .Distinct()

这篇关于Roslyn 获取类的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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