2

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?

Mister Epic
  • 16,295
  • 13
  • 76
  • 147

1 Answers1

1

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

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

Dudi Keleti
  • 2,946
  • 18
  • 33