Roslyn服务为空

时间:2017-02-26 16:25:05

标签: c# roslyn

我在这里做错了什么?以下代码失败(并且它来自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);
    }
}

2 个答案:

答案 0 :(得分:4)

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

        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);

答案 1 :(得分:0)

添加Microsoft.CodeAnalysis.CSharp.Features NuGet软件包为我解决了该问题。