I am using Roslyn to analyze C# code. One of the things I need to have is analyzing a class declaration node and get information about:
I can get access to the class declaration node (type ClassDeclarationSyntax
), and from there I can get access to the BaseList
:
ClassDeclarationSyntax node = ...; // The class declaration
BaseListSyntax baseList = node.BaseList;
However baseList
contains both interfaces and classes. I need ti distinguish classes from interfaces. How?
SemanticModel
?I've searched Roslyn's Wiki and discovered that it is 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 those info from here? Thanks
Yes.
The syntax tree just knows what words are where; it doesn't know anything about what an identifier refers to.
You need to get the SemanticModel from the compilation), then call GetSymbolInfo()
on each identifier node in the list. You can then cast the symbol to ITypeSymbol
to find out about the type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With