public override void Initialize(AnalysisContext context) { context.RegisterCompilationStartAction(compilationContext => { // Check if the attribute type marking unsecure methods is defined. var unsecureMethodAttributeType = compilationContext.Compilation.GetTypeByMetadataName(UnsecureMethodAttributeName); if (unsecureMethodAttributeType == null) { return; } // Check if the interface type marking secure types is defined. var secureTypeInterfaceType = compilationContext.Compilation.GetTypeByMetadataName(SecureTypeInterfaceName); if (secureTypeInterfaceType == null) { return; } // Initialize state in the start action. var analyzer = new CompilationAnalyzer(unsecureMethodAttributeType, secureTypeInterfaceType); // Register an intermediate non-end action that accesses and modifies the state. compilationContext.RegisterSymbolAction(analyzer.AnalyzeSymbol, SymbolKind.NamedType, SymbolKind.Method); // Register an end action to report diagnostics based on the final state. compilationContext.RegisterCompilationEndAction(analyzer.CompilationEndAction); }); }