Getting Class FullName (including namespace) from Roslyn ClassDeclarationSyntax

Viewed 11237

I've a ClassDeclarationSyntax from a syntax tree in roslyn. I read it like this:

var tree = SyntaxTree.ParseText(sourceCode);
var root = (CompilationUnitSyntax)tree.GetRoot();

var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();

The identifier only contains the name of the class but no information about the namespace, so the fullType Name is missing. Like "MyClass" but noch "Namespace1.MyClass"

what is the recommended way to get the namespace / FulltypeName of the Syntax?

9 Answers

I know I am rather late in the game, but I stumbled upon one of given the answers which did not work in my case where I had to deal with nested classes. So therefore here is a method that will handle nested classes as well:

public static class ClassDeclarationSyntaxExtensions
{
    public const string NESTED_CLASS_DELIMITER = "+";
    public const string NAMESPACE_CLASS_DELIMITER = ".";

    public static string GetFullName(this ClassDeclarationSyntax source)
    {
        Contract.Requires(null != source);

        var items = new List<string>();
        var parent = source.Parent;
        while (parent.IsKind(SyntaxKind.ClassDeclaration))
        {
            var parentClass = parent as ClassDeclarationSyntax;
            Contract.Assert(null != parentClass);
            items.Add(parentClass.Identifier.Text);

            parent = parent.Parent;
        }

        var nameSpace = parent as NamespaceDeclarationSyntax;
        Contract.Assert(null != nameSpace);
        var sb = new StringBuilder().Append(nameSpace.Name).Append(NAMESPACE_CLASS_DELIMITER);
        items.Reverse();
        items.ForEach(i => { sb.Append(i).Append(NESTED_CLASS_DELIMITER); });
        sb.Append(source.Identifier.Text);

        var result = sb.ToString();
        return result;
    }
}

Based on the answer by Ronald, I added support for: nested namespaces, structs and generics.

It uses the generics CLR naming convention to allow the output to be used as input of Compilation.GetTypeByMetadataName()

public static class TypeDeclarationSyntaxExtensions
{
    const char NESTED_CLASS_DELIMITER = '+';
    const char NAMESPACE_CLASS_DELIMITER = '.';
    const char TYPEPARAMETER_CLASS_DELIMITER = '`';

    public static string GetFullName(this TypeDeclarationSyntax source)
    {
        if (source is null)
            throw new ArgumentNullException(nameof(source));

        var namespaces = new LinkedList<BaseNamespaceDeclarationSyntax>();
        var types = new LinkedList<TypeDeclarationSyntax>();
        for (var parent = source.Parent; parent is object; parent = parent.Parent)
        {
            if (parent is BaseNamespaceDeclarationSyntax @namespace)
            {
                namespaces.AddFirst(@namespace);
            }
            else if (parent is TypeDeclarationSyntax type)
            {
                types.AddFirst(type);
            }
        }

        var result = new StringBuilder();
        for (var item = namespaces.First; item is object; item = item.Next)
        {
            result.Append(item.Value.Name).Append(NAMESPACE_CLASS_DELIMITER);
        }
        for (var item = types.First; item is object; item = item.Next)
        {
            var type = item.Value;
            AppendName(result, type);
            result.Append(NESTED_CLASS_DELIMITER);
        }
        AppendName(result, source);

        return result.ToString();
    }

    static void AppendName(StringBuilder builder, TypeDeclarationSyntax type)
    {
        builder.Append(type.Identifier.Text);
        var typeArguments = type.TypeParameterList?.ChildNodes()
            .Count(node => node is TypeParameterSyntax) ?? 0;
        if (typeArguments != 0)
            builder.Append(TYPEPARAMETER_CLASS_DELIMITER).Append(typeArguments);
    }
}

There's also an elegant way when using pattern matching + recursion:

// method with pattern matching
public static string GetNamespaceFrom(SyntaxNode s) =>
    s.Parent switch
    {
        NamespaceDeclarationSyntax namespaceDeclarationSyntax => namespaceDeclarationSyntax.Name.ToString(),
        null => string.Empty, // or whatever you want to do
        _ => GetNamespaceFrom(s.Parent)
    };

// somewhere call it passing the class declaration syntax:
string ns = GetNamespaceFrom(classDeclarationSyntax);

Yet another answer.. This utility class supports nested namespaces and nested classes as well.

public static class SyntaxNodeHelper
{
    public static string GetPrefix(SyntaxNode member)
    {
        if (member == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        SyntaxNode node = member;

        while(node.Parent != null) {
            node = node.Parent;

            if (node is NamespaceDeclarationSyntax) {
                var namespaceDeclaration = (NamespaceDeclarationSyntax) node;

                sb.Insert(0, ".");
                sb.Insert(0, namespaceDeclaration.Name.ToString());
            } else if (node is ClassDeclarationSyntax) {
                var classDeclaration = (ClassDeclarationSyntax) node;

                sb.Insert(0, ".");
                sb.Insert(0, classDeclaration.Identifier.ToString());
            }
        }

        return sb.ToString();
    }
}

This code could solve your problem.

public static string ClassFullName(this ClassDeclarationSyntax varClassDec)
{
    SyntaxNode tempCurCls = varClassDec;
    var tempFullName = new Stack<string>();
    
    do
    {
        if (tempCurCls.Kind() == SyntaxKind.ClassDeclaration)
        {
            tempFullName.Push(((ClassDeclarationSyntax)tempCurCls).Identifier.ToString());
        }
        else if (tempCurCls.Kind() == SyntaxKind.NamespaceDeclaration)
        {
            tempFullName.Push(((NamespaceDeclarationSyntax)tempCurCls).Name.ToString());
        }
        
        tempCurCls = tempCurCls.Parent;
    } while (tempCurCls != null);
    
    return string.Join(".", tempFullName);
}
Related