SonarQube.Plugins.Roslyn.DiagnosticAssemblyScanner.InstantiateDiagnostics C# (CSharp) Method

InstantiateDiagnostics() public method

Loads all of the given assemblies and instantiates Roslyn diagnostic objects - i.e. existing types deriving from DiagnosticAnalyzer. Non-assembly files will be ignored.
public InstantiateDiagnostics ( string language ) : IEnumerable
language string
return IEnumerable
        public IEnumerable<DiagnosticAnalyzer> InstantiateDiagnostics(string language, params string[] files)
        {
            // If there were any additional assembly search directories specified in the constructor, use them
            AssemblyResolver additionalAssemblyResolver = null;
            if (additionalSearchFolders.Any())
            {
                additionalAssemblyResolver = new AssemblyResolver(this.logger, additionalSearchFolders.ToArray());
            }

            List<DiagnosticAnalyzer> analyzers = new List<DiagnosticAnalyzer>();

            try
            {
                foreach (string assemblyPath in files.Where(f => Utilities.IsAssemblyLibraryFileName(f)))
                {
                    analyzers.AddRange(InstantiateDiagnosticsFromAssembly(assemblyPath, language));
                }

            }
            finally
            {
                // Dispose of the AssemblyResolver instance, if applicable
                if (additionalAssemblyResolver != null)
                {
                    additionalAssemblyResolver.Dispose();
                }
            }
            return analyzers;
        }

Usage Example

        /// <summary>
        /// Retrieves the analyzers contained within a given NuGet package corresponding to a given language
        /// </summary>
        private IEnumerable <DiagnosticAnalyzer> GetAnalyzers(IPackage package, string language)
        {
            string packageRootDir         = this.packageHandler.GetLocalPackageRootDirectory(package);
            string additionalSearchFolder = this.packageHandler.LocalCacheRoot;

            this.logger.LogInfo(UIResources.APG_LocatingAnalyzers);
            string[] analyzerFiles = Directory.GetFiles(packageRootDir, "*.dll", SearchOption.AllDirectories);

            string roslynLanguageName = SupportedLanguages.GetRoslynLanguageName(language);

            this.logger.LogDebug(UIResources.APG_LogAnalyzerLanguage, roslynLanguageName);

            DiagnosticAssemblyScanner        diagnosticAssemblyScanner = new DiagnosticAssemblyScanner(this.logger, additionalSearchFolder);
            IEnumerable <DiagnosticAnalyzer> analyzers = diagnosticAssemblyScanner.InstantiateDiagnostics(roslynLanguageName, analyzerFiles.Where(x => !x.Contains("Microsoft.CodeAnalysis")).ToArray());

            return(analyzers);
        }
All Usage Examples Of SonarQube.Plugins.Roslyn.DiagnosticAssemblyScanner::InstantiateDiagnostics