If using System.Linq;
is present and System.Linq
is among the referenced assemblies, I expect Completions on an int[] array
to return LINQ extension methods, e.g. Select<>(...)
, Where<>(...)
, etc. In fact I am only getting public methods and properties of the int[]
type.
Here is the full code:
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;
}
Am I doing something wrong?