罗斯林:如何获得候选命名空间尚未解决的类型 [英] Roslyn: How to get candidate namespaces for unresolved types

查看:223
本文介绍了罗斯林:如何获得候选命名空间尚未解决的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用罗斯林,是有办法,我可以得到的候选名称空间的列表中的每个未解决的符号列表?如果是这样,是有办法,我可以做一个最佳匹配与模糊性的符号,它属于多种可能的命名空间?

Using Roslyn, is there a way I can get the list of candidate namespaces for each unresolved symbols in a list? If so, is there a way I can do a 'best match' for symbols with ambiguity, which belong to multiple possible namespaces?

我想用生成在文件中未解决的符号语句列表。我能够使用如罗斯林办法的语义信息获取的未解决的符号:如何让未解决的类型的,但不能找到一种方法,在其命名空间从引用的程序集的项目,这些符号到达。

I would like to generate a list of using statements for the unresolved symbols in a file. I am able to get the unresolved symbols from the semantic information using an approach like Roslyn : How to get unresolved types, but could not find a way to arrive at the namespaces for these symbols from the referenced assemblies in the project.

推荐答案

我掠过罗斯林回购,它看起来像他们所使用的 SymbolFinder 来检索信息时,他们认为,用户缺少使用:请参见<一href="https://github.com/dotnet/roslyn/blob/03e30451ce7eb518e364b5806c524623424103e4/src/Features/Core/$c$cFixes/Async/AbstractAddAsync$c$cFixProvider.cs#L40-L49"相对=nofollow>这里

I skimmed the Roslyn Repo and it looks like they use the SymbolFinder to retrieve information when they believe the user is missing a using: See here.

对于寻找最佳的比赛,我相信这件事情你必须根据你认为什么是最好的匹配来实现。 Visual Studio中只显示你所有的候选使用语句。

As for finding the "best" match, I believe that's something you'll have to implement according to what you consider to be the "best" match. Visual Studio simply shows you all candidate using statements.

下面是一个示例我扔一起快速地展示 SymbolFinder

Here's a sample I threw together quickly to demonstrate SymbolFinder:

var ws = new AdhocWorkspace();
var solutionInfo = SolutionInfo.Create(SolutionId.CreateNewId();, VersionStamp.Create());
var solution = ws.AddSolution(solutionInfo);
var project = ws.AddProject("Sample", "C#");

//Add reference to mscorlib
var mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
project = project.AddMetadataReference(mscorlib);
ws.TryApplyChanges(project.Solution);

string text = @"
class C
{
    void M()
    {
        //Missing a using System;
        Console.Write();
    }
}";
var sourceText = SourceText.From(text);

//Add document to project
var doc = ws.AddDocument(project.Id, "NewDoc", sourceText);
var model = doc.GetSemanticModelAsync().Result;
var unresolved = doc.GetSyntaxRootAsync().Result.DescendantNodes().OfType<IdentifierNameSyntax>()
   .Where(x => model.GetSymbolInfo(x).Symbol == null);

foreach (var identifier in unresolved)
{
    var candidateUsings = SymbolFinder.FindDeclarationsAsync(doc.Project, identifier.Identifier.ValueText, ignoreCase: false).Result;

    //Process candidate usings...
}

这篇关于罗斯林:如何获得候选命名空间尚未解决的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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