Roslyn 上 GetCompletionsAsync 的 NullReferenceException

NullReferenceException at GetCompletionsAsync on Roslyn

我尝试获取代码完成,但是当我尝试通过 CompletionService 获取异步完成时,我得到 NullReferenceException

这是一段代码:

public async Task<IList<ICompletionData>> GetCompletionData(String code, int offset)
    {
        IList<ICompletionData> completionData = null;

        if (_workspace != null)
        {
            _workspace.Dispose();
        }
        _workspace = new AdhocWorkspace();
        _project = _workspace.AddProject("Test", LanguageNames.CSharp);



        var sourceText = SourceText.From(code);
        var document = _workspace.AddDocument(_project.Id, "Compilation0", sourceText); 
        var completionService = CompletionService.GetService(document);
        var charCompletion = GetCompletionTrigger('.');
        var data = await completionService.GetCompletionsAsync(_document, offset,charCompletion,).ConfigureAwait(false);

        if (data == null || data.Items.Any() == false)
            return new List<ICompletionData>();

        completionData = data.Items.Select(item => new RoslynCodeCompletion(_document, item)).ToList<ICompletionData>();

        return completionData;
    }

行错误:

var data = await completionService.GetCompletionsAsync(_document, offset,charCompletion,).ConfigureAwait(false);//NullReferenseException.

脚本代码代码完成由“.”触发(点)符号:

var str="";
str. // here calls `GetCompletionData`

完整的代码片段在 pastebin

Roslyn PR #14921 was merged. Before you had to a lot of manual MEF configuration 之后这变得容易多了,但现在安装以下 nuget 包应该就足够了:

  • Microsoft.CodeAnalysis
  • Microsoft.CodeAnalysis.CSharp
  • Microsoft.CodeAnalysis.特征
  • Microsoft.CodeAnalysis.CSharp.Features

Roslyn 将进行所需的配置。