Using Roslyn I'm trying to interpret some code that looks something like this:
public class Foo
{
public override Type BarType
{
get { return typeof(MyBar); }
}
}
What I would like to do is get MyBar and then get that type as a symbol, but I'm not sure if this is even something that is possible or practical to do. I'm going to have several classes that look like this and all derive from a base class.
Given the ClassDeclarationSyntax for Foo, I can do this:
var prop = syntax.DescendantNodes().OfType<PropertyDeclarationSyntax>()
.FirstOrDefault(p => p.Identifier.ToString() == "BarType");
Or given the INamedTypeSymbol for Foo, I can do this:
var member = symbol.GetMembers("BarType").FirstOrDefault();
But I don't know where to go from there.
Ultimately I want to be able to get the symbol for MyBar for further analysis, so maybe even getting the string "MyBar" isn't going to help because it's not fully qualified.
Any suggestions?
EDIT:
I'm getting a project and a compilation like this:
var workspace = MSBuildWorkspace.Create();
var project = workspace.OpenProjectAsync(projectPath).Result;
var compilation = project.GetCompilationAsync().Result;
compilation is a CSharpCompilation here. From there I do something like this:
foreach (var doc in project.Documents)
{
Console.WriteLine($"Analyzing {doc.Name}");
//var model = doc.GetSemanticModelAsync().Result;
var tree = doc.GetSyntaxTreeAsync().Result;
var root = tree.GetRoot();
var model = compilation.GetSemanticModel(tree);
var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
foreach (var syntax in classes)
{
var symbol = model.GetDeclaredSymbol(syntax);
//... need to analyze properties in the class here...
}
}
Either way I get model I end up with a SyntaxTreeSemanticModel which doesn't seem to have a GetTypeSymbol method.