public void GetSymbolsForAccessibleMembersOfAType()
{
var source = @"
using System;
public class C
{
internal int InstanceField = 0;
public int InstanceProperty { get; set; }
internal void InstanceMethod()
{
Console.WriteLine(InstanceField);
}
protected void InaccessibleInstanceMethod()
{
Console.WriteLine(InstanceProperty);
}
}
public static class ExtensionMethods
{
public static void ExtensionMethod(this C s)
{
}
}
class Program
{
static void Main()
{
C c = new C();
c.ToString();
}
}";
var tree = SyntaxTree.ParseText(source);
var compilation = Compilation.Create("MyCompilation")
.AddReferences(MetadataReference.CreateAssemblyReference("mscorlib"))
.AddSyntaxTrees(tree);
var model = compilation.GetSemanticModel(tree);
// Get position of 'c.ToString()' above.
var position = source.IndexOf("c.ToString()");
// Get IdentifierNameSyntax corresponding to identifier 'c' above.
var identifier = (IdentifierNameSyntax)tree.GetRoot().FindToken(position).Parent;
// Get TypeSymbol corresponding to variable 'c' above.
TypeSymbol type = model.GetTypeInfo(identifier).Type;
// Get symbols for 'accessible' members on the above TypeSymbol.
// To also include inacessible memebers, use LookupOptions.IgnoreAccessibility.
ReadOnlyArray<Symbol> symbols = model.LookupSymbols(position, container: type,
options: LookupOptions.MustBeInstance | LookupOptions.IncludeExtensionMethods);
var results = string.Join("\r\n", symbols
.Select(symbol => symbol.ToDisplayString())
.OrderBy(result => result));
Assert.AreEqual(@"C.ExtensionMethod()
C.InstanceField
C.InstanceMethod()
C.InstanceProperty
object.Equals(object)
object.GetHashCode()
object.GetType()
object.ToString()", results);
}