gpt4 book ai didi

c# - Roslyn 服务为空

转载 作者:行者123 更新时间:2023-12-01 15:02:56 26 4
gpt4 key购买 nike

我在这里做错了什么?以下代码失败(取自 Roslyn 的源网站)

public class CompletionServiceTests
{
public void AcquireCompletionService()
{
var workspace = new AdhocWorkspace();

var document = workspace
.AddProject("TestProject", LanguageNames.CSharp)
.AddDocument("TestDocument.cs", "");

var service = CompletionService.GetService(document);
Assert.NotNull(service);
}
}

最佳答案

我发现了问题。我需要这样做:

        var assemblies = new[]
{
Assembly.Load("Microsoft.CodeAnalysis"),
Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
Assembly.Load("Microsoft.CodeAnalysis.Features"),
Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
};

var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
.Distinct()
.SelectMany(x => x.GetTypes())
.ToArray();

var compositionContext = new ContainerConfiguration()
.WithParts(partTypes)
.CreateContainer();

var host = MefHostServices.Create(compositionContext);

var workspace = new AdhocWorkspace(host);

var document = workspace
.AddProject("TestProject", LanguageNames.CSharp)
.AddDocument("TestDocument.cs", "");

var service = CompletionService.GetService(document);

关于c# - Roslyn 服务为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471015/

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