public void GetInScopeSymbols()
{
var source = @"
class C
{
}
class Program
{
private static int i = 0;
public static void Main()
{
int j = 0; j += i;
// What symbols are in scope here?
}
}";
var tree = SyntaxTree.ParseText(source);
var mscorlib = MetadataReference.CreateAssemblyReference("mscorlib");
var compilation = Compilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { mscorlib });
var model = compilation.GetSemanticModel(tree);
// Get position of the comment above.
var position = source.IndexOf("//");
// Get 'all' symbols that are in scope at the above position.
ReadOnlyArray<Symbol> symbols = model.LookupSymbols(position);
var results = string.Join("\r\n", symbols.Select(symbol => symbol.ToDisplayString()).OrderBy(s => s));
Assert.AreEqual(@"C
j
Microsoft
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Program
Program.i
Program.Main()
System", results);
// Filter results using LookupOptions (get everything except instance members).
symbols = model.LookupSymbols(position, options: LookupOptions.MustNotBeInstance);
results = string.Join("\r\n", symbols.Select(symbol => symbol.ToDisplayString()).OrderBy(s => s));
Assert.AreEqual(@"C
j
Microsoft
object.Equals(object, object)
object.ReferenceEquals(object, object)
Program
Program.i
Program.Main()
System", results);
// Filter results by looking at Kind of returned symbols (only get locals and fields).
results = string.Join("\r\n", symbols
.Where(symbol => symbol.Kind == SymbolKind.Local || symbol.Kind == SymbolKind.Field)
.Select(symbol => symbol.ToDisplayString()).OrderBy(s => s));
Assert.AreEqual(@"j
Program.i", results);
}