How to tell classes from interfaces in Roslyn `BaseList`?

I use Roslyn to parse C # code. One of the things I need is to parse the node class declaration and get information about:

  • Own base class
  • Introduced Interfaces

I can access the declaration of the node (type ClassDeclarationSyntax) class , and from there I can access BaseList:

ClassDeclarationSyntax node = ...; // The class declaration
BaseListSyntax baseList = node.BaseList;

However, it BaseListcontains both interfaces and classes. I need to distinguish classes from interfaces. How?

Do I need to use SemanticModel?

I searched the Roslyn Wiki and found that it was possible to access semantic information from my AST.

SyntaxTree programRoot = ...; // Getting the AST root
CSharpCompilation compilation = CSharpCompilation.Create("Program")
    .AddReferences(MetadataReference.CreateFromFile(
    typeof(object).Assembly.Location))
    .AddSyntaxTrees(programRoot);

But how to get information from here? Thanks

+4
1

.

, ; , .

SemanticModel ), GetSymbolInfo() node . ITypeSymbol, .

+3

Source: https://habr.com/ru/post/1610198/


All Articles