Mister Epic
Mister Epic

Reputation: 16713

Can't retrieve type information via Roslyn

I am attempting to retrieve the type of a class syntax node in Roslyn so I can get the enclosing namespace by following along with @slaks answer: Roslyn : How to get the Namespace of a DeclarationSyntax with Roslyn C#

I have the following:

static async Task MainAsync(string[] args)
{
    string projectPath = @"C:\Projects\ertp\Ertp.Mobile.csproj";
    var msWorkspace = MSBuildWorkspace.Create();

    var project = msWorkspace.OpenProjectAsync(projectPath).Result;

    foreach (var document in project.Documents)
    {
        Console.WriteLine(project.Name + "\t\t\t" + document.Name);
        SemanticModel model = await document.GetSemanticModelAsync();
        var classes = document.GetSyntaxRootAsync().Result.DescendantNodes().OfType<ClassDeclarationSyntax>();
        foreach (var klass in classes)
        {
            var info = model.GetTypeInfo(klass);
            var isNull = info.Type == null; //TRUE
        }
    }

If I can't get the type I can't the namespace - any idea how I can retrieve the details I require?

Upvotes: 2

Views: 119

Answers (1)

Dudi Keleti
Dudi Keleti

Reputation: 2984

For decelerators you need to call model.GetDeclaredSymbol(node) and then for the namespace, ContainingNamespace.

model.GetTypeInfo(node).Type will work for expression node.

Upvotes: 1

Related Questions