c# - Roslyn CompletionService 不返回静态扩展方法

标签 c# roslyn

如果'使用 System.Linq;'存在并且 System.Linq 是引用的程序集之一,我希望 int[] 数组上的 Completions 返回 LINQ 扩展方法,例如Select<>(...), Where<>(...) 等。事实上,我只获取 int[] 类型的公共(public)方法和属性。
这是完整的代码:

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

难道我做错了什么?

最佳答案

事实证明,我必须添加对构成 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,
        });

之后一切都开始工作了。

关于c# - Roslyn CompletionService 不返回静态扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59791893/

相关文章:

C#:从外部程序获取数据

c# - 在不修改属性的情况下复制文件夹/文件?

c# - 在应用 Mode-View-ViewModel 设计模式时包括部分 View

c# - 存在项目引用时,不会填充项目元数据引用

javascript - 如何将回调功能添加到 signalR 中的 hubClass 以将当前工作状态更新到客户端并延迟?

c# - 在 iText7 上正确对齐

c# - 如何使用 SemanticModel 检查变量是否已经过测试?

c# - 如何判断分部方法没有实现

c# - 使用 Roslyn 拆分表达式语句

c# - 为什么 ITypeSymbol 中有静态构造函数的集合而不是一个?