Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get class semantic model from class syntax tree?

Tags:

c#

roslyn

How can I get class semantic model (ITypeSymbol) from ClassDeclarationSyntax in Roslyn? From this syntax tree:

enter image description here

it seems to me that the only point I can use is ClassDeclaration as tokens like IdentifierToken cannot be passed to the GetSymbolInfo method. But when I write

context.SemanticModel.GetSymbolInfo(classDeclaration)

the result is

context.SemanticModel.GetSymbolInfo(classDeclaration)
{Microsoft.CodeAnalysis.SymbolInfo}
    CandidateReason: None
    CandidateSymbols: Length = 0
    IsEmpty: true
    Symbol: null
    _candidateSymbols: Length = 0

... so no match. I wonder if the problem is that I am asking for wrong syntax element, or the issue is in fact I am asking in the moment I analyze attribute of the class and the class itself is not yet prepared.

like image 767
Lukáš Lánský Avatar asked Oct 30 '15 14:10

Lukáš Lánský


1 Answers

You can use the SemanticModel.GetDeclaredSymbol().

like image 176
Tamas Avatar answered Sep 22 '22 04:09

Tamas