获得语义模型
有一些方法来获得 sematic 模型。
-
来自
Document
级Document document = ...; SemanticModel semanticModel = await document.GetSemanticModelAsync();
-
来自
Compilation
classCSharpCompilation compilation = ...; var semanticModel = await compilation.GetSemanticModel(syntaxTree);
-
从一个
AnalysisContext
。例如,在DiagnosticAnalyzer
中你可以做到:public override void Initialize(AnalysisContext context) { context.RegisterSemanticModelAction(x => { var semanticModel = x.SemanticModel; // Do magical magic here. }); }