gpt4 book ai didi

c# - Roslyn 上 GetCompletionsAsync 的 NullReferenceException

转载 作者:行者123 更新时间:2023-11-30 23:19:45 28 4
gpt4 key购买 nike

我尝试获取代码完成,但是当我尝试通过 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 之后这变得容易多了被合并了。在你需要很多manual MEF configuration之前,但现在安装以下 nuget 包应该就足够了:

  • Microsoft.CodeAnalysis
  • Microsoft.CodeAnalysis.CSharp
  • Microsoft.CodeAnalysis.Features
  • Microsoft.CodeAnalysis.CSharp.Features

Roslyn 将进行所需的配置。

关于c# - Roslyn 上 GetCompletionsAsync 的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100985/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com