C
C#2mo ago
Davaaron

✅ Roslyn: Get file scoped namespace

Hi, i'm using the new file scoped namespace
using System;

namespace TestNamespaceSpace;

public class TestClass
{

}
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();
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?
6 Replies
Pobiega
Pobiega2mo ago
using namespace TestNameSpace;? is it really that, or did you just typo the using?
Davaaron
Davaaron2mo ago
That was just a typo, sorry, but good catch But dammit, i found the issue 😄 they surprisingly added a new class for file-scoped namespaces... FileScopedNamespaceDeclarationSyntax
Marvin
Marvin2mo ago
Researhing a good method to get namespace for every situation, i came across a method like this, maybe it helps in the future too
C#
public static string GetNamespace(
BaseTypeDeclarationSyntax syntax) {

string nameSpace = string.Empty;
SyntaxNode? potentialNamespaceParent = syntax.Parent;

while(potentialNamespaceParent is not null
and not NamespaceDeclarationSyntax
and not FileScopedNamespaceDeclarationSyntax) {

potentialNamespaceParent = potentialNamespaceParent.Parent;

}

if(potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent) {

nameSpace = namespaceParent.Name.ToString();

while(namespaceParent.Parent is NamespaceDeclarationSyntax parent) {

nameSpace = $"{namespaceParent.Name}.{nameSpace}";
namespaceParent = parent;

}

}

return nameSpace;

}
C#
public static string GetNamespace(
BaseTypeDeclarationSyntax syntax) {

string nameSpace = string.Empty;
SyntaxNode? potentialNamespaceParent = syntax.Parent;

while(potentialNamespaceParent is not null
and not NamespaceDeclarationSyntax
and not FileScopedNamespaceDeclarationSyntax) {

potentialNamespaceParent = potentialNamespaceParent.Parent;

}

if(potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent) {

nameSpace = namespaceParent.Name.ToString();

while(namespaceParent.Parent is NamespaceDeclarationSyntax parent) {

nameSpace = $"{namespaceParent.Name}.{nameSpace}";
namespaceParent = parent;

}

}

return nameSpace;

}
Davaaron
Davaaron2mo ago
Thanks for sharing your code! 🙂 That's what i was planning to do right now, like traversing the parents, because I figured out the file scoped namespace syntax is not a sibling of the class declaration syntax, but a parent.. that's kinda wild 😄 but it also makes sense logically your code seems to fit my needs, thanks 🙂
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server