'How can I get the value of inheritance class of other inheritance class in .NET Roslyn API?

Good day, I've been trying to get the value of inheritance class name of other inheritance class in Roslyn API.

like below picture, enter image description here

when I'm scanning the 'Face' class, I want to get the name of "Human". like this structure, I can get the first inheritance class name by using ClassDeclaration.BaseList. I can get the name of "Head" from Face class!

enter image description here

But I can't access the second inheritance class(Human class). I think that there's no more tree structure in Face class.

The question point is that the way how to get 2-layer upper(or more) inheritance class name if in case of the classes are seperated.

Thank you.



Solution 1:[1]

Get the SemanticModel for your tree, then call GetDeclaredSymbol() with the ClassDeclarationSyntax. That'll give you the ITypeSymbol and you can look at BaseType from there. You don't want to try this with syntax only because of partial classes.

Solution 2:[2]

create a recursive function on BaseList property of SyntaxNode. somthing like this

private static bool IsDrivedFromHead(ClassDeclarationSyntax syntax)
        {
            if (syntax.BaseList == null)
                return false;
            if (!syntax.BaseList.Types.Any(a => a.Type.ToString() == "Namespace.Head"))
            {
                bool IsDrived = false;
                foreach (var baseType in syntax.BaseList.Types)
                {
                    var node = (CompilationUnitSyntax)baseType.SyntaxTree.GetRoot();
                    foreach (ClassDeclarationSyntax member in node.Members.Where(w=> w is ClassDeclarationSyntax))
                    {
                        IsDrived = IsInheritFromHead(member);
                        if (IsDrived)
                            break;
                    }
                    if (IsDrived)
                        break;
                }
                return IsDrived;
            }
            else
                return true;
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason Malinowski
Solution 2 Ehsan Vali