Search code examples
c#sonarquberoslyncode-inspection

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

  • 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.