There qutie a fiew ways to get the sematic model.
From a Document
class
Document document = ...;
SemanticModel semanticModel = await document.GetSemanticModelAsync();
From a Compilation
class
CSharpCompilation compilation = ...;
var semanticModel = await compilation.GetSemanticModel(syntaxTree);
From an AnalysisContext
. Fro example inside a DiagnosticAnalyzer
you can do:
public override void Initialize(AnalysisContext context)
{
context.RegisterSemanticModelAction(x =>
{
var semanticModel = x.SemanticModel;
// Do magical magic here.
});
}