C#C
C#16mo ago
Davaaron

✅ Roslyn: Get file scoped namespace

Hi, i'm using the new file scoped namespace

using System;

namespace TestNamespaceSpace;

public class TestClass 
{

}


I try to extract the namespace like this
var root = syntaxTree.GetRoot();
var controllerClasses = root.DescendantNodes().OfType<ClassDeclarationSyntax>()
                            .Where(IsControllerClass);

var @namespace = root.DescendantNodesAndSelf().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();

but @namespace is null. Why is that?
Was this page helpful?