コード例 #1
0
        /// <summary>
        /// Return <see cref="DiagnosticAnalyzer.SupportedDiagnostics"/> of given <paramref name="analyzer"/>.
        /// </summary>
        public ImmutableArray <DiagnosticDescriptor> GetSupportedDiagnosticDescriptors(
            DiagnosticAnalyzer analyzer,
            AnalyzerExecutor analyzerExecutor)
        {
            var descriptors = _descriptorCache.GetOrAdd(analyzer, key =>
            {
                var supportedDiagnostics = ImmutableArray <DiagnosticDescriptor> .Empty;

                // Catch Exception from analyzer.SupportedDiagnostics
                analyzerExecutor.ExecuteAndCatchIfThrows(analyzer, () => { supportedDiagnostics = analyzer.SupportedDiagnostics; });

                EventHandler <Exception> handler = null;
                Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = analyzerExecutor.OnAnalyzerException;
                if (onAnalyzerException != null)
                {
                    handler = new EventHandler <Exception>((sender, ex) =>
                    {
                        var diagnostic = AnalyzerExecutor.GetAnalyzerExceptionDiagnostic(analyzer, ex);
                        onAnalyzerException(ex, analyzer, diagnostic);
                    });

                    // Subscribe for exceptions from lazily evaluated localizable strings in the descriptors.
                    foreach (var descriptor in supportedDiagnostics)
                    {
                        descriptor.Title.OnException         += handler;
                        descriptor.MessageFormat.OnException += handler;
                        descriptor.Description.OnException   += handler;
                    }
                }

                return(Tuple.Create(supportedDiagnostics, handler));
            });

            return(descriptors.Item1);
        }