Roslyn CompletionService 不返回静态扩展方法

Nic*_*yak 6 c# roslyn

如果using System.Linq;存在并且System.Linq位于引用的程序集中,我希望 an 上的int[] array完成返回 LINQ 扩展方法,例如Select<>(...)Where<>(...)等。事实上,我只获取该int[]类型的公共方法和属性。这是完整的代码:

static void Main(string[] args)
    {
        string code = @"using System;
        using System.Linq;

       namespace RoslynCompletionTests
       {
             public static class MyTestClass1
             {
                   public static void Print()
                   {
                          int[] array = {1,2,3,4,5,6};

                          var result = array.Select(i => new { I = i }).Select(v => v.I);
                   }
             }
       }";
        var host = MefHostServices.Create(MefHostServices.DefaultAssemblies);
        
        Type[] types =
        {
            typeof(object),
            typeof(Enumerable),
            typeof(IEnumerable),
            typeof(Console),
            typeof(Assembly),
            typeof(List<>),
            typeof(Type)
        };

        ImmutableArray<string> imports = types.Select(x => x.Namespace).Distinct().ToImmutableArray();

        ImmutableArray<MetadataReference> references =
            types.Select(t => MetadataReference.CreateFromFile(t.Assembly.Location) as MetadataReference)
                 .ToImmutableArray();

        AdhocWorkspace workspace = new AdhocWorkspace(host, "Custom");

        string name = "MyTestProj";

        ProjectId id = ProjectId.CreateNewId(name);

        ParseOptions parseOptions = new CSharpParseOptions();

        CompilationOptions compilationOptions =
            new CSharpCompilationOptions
            (
                OutputKind.DynamicallyLinkedLibrary,
                usings: imports,
                allowUnsafe: true);

        ProjectInfo projInfo =
            ProjectInfo.Create
            (
                id,
                VersionStamp.Create(),
                name,
                name,
                LanguageNames.CSharp,
                parseOptions: parseOptions,
                compilationOptions: compilationOptions,
                metadataReferences: references);

        Project proj = workspace.AddProject(projInfo);

        SourceText text = SourceText.From(code);

        Document doc = proj.AddDocument("MyDoc.cs", text);

        SemanticModel semanticModel = doc.GetSemanticModelAsync().Result;

        CompletionService completionService = CompletionService.GetService(doc);

        string strToFind = "array.";
        int idx = text.ToString().IndexOf(strToFind) + strToFind.Length;

        var results = completionService.GetCompletionsAsync(doc, idx).Result;
    }
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

Nic*_*yak 4

事实证明,我必须添加对形成以下内容的某些程序集的引用MetadataReferences

var assemblies = types.Select(t => t.Assembly).Concat(new[]
{
     Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
     typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly,
});
Run Code Online (Sandbox Code Playgroud)

之后一切开始工作。