Ejemplo n.º 1
0
        /// <summary>
        /// Searches the specified assembly for Analyzer instances and loads them.
        /// </summary>
        /// <param name="analyzerAssembly">The assembly to consume.</param>
        protected void ConsumeAssembly(Assembly analyzerAssembly)
        {
            if (analyzerAssembly == null)
            {
                throw new ArgumentNullException(nameof(analyzerAssembly));
            }

            AnalyzerLoadContext loadContext = new AnalyzerLoadContext();

            AnalyzerProviderAttribute attr =
                (AnalyzerProviderAttribute)analyzerAssembly.GetCustomAttribute(typeof(AnalyzerProviderAttribute));

            if (attr != null && attr.ProviderType != null)
            {
                IAnalyzerProvider analyzerProvider = Activator.CreateInstance(attr.ProviderType) as IAnalyzerProvider;
                if (analyzerProvider != null)
                {
                    foreach (Analyzer analyzer in analyzerProvider.GetAnalyzers())
                    {
                        loadContext.Reset(analyzer);
                        OnAnalyzerLoaded(loadContext);
                        if (loadContext.ShouldRun)
                        {
                            _analyzers.Add(loadContext.Analyzer);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal static IEnumerable <Analyzer> GetAnalyzers()
        {
#if AUTOANALYSIS_EXTENSIBILITY
            // Iterate through all assemblies in the analyzers directory.
            if (!Directory.Exists(AnalyzersDirectory))
            {
                yield break;
            }

            string[] candidateAssemblies = Directory.GetFiles(AnalyzersDirectory, "*.dll", SearchOption.TopDirectoryOnly);
            foreach (string candidateAssembly in candidateAssemblies)
            {
                Assembly assembly = Assembly.LoadFrom(candidateAssembly);
                if (assembly != null)
                {
                    AnalyzerProviderAttribute attr =
                        (AnalyzerProviderAttribute)assembly.GetCustomAttribute(typeof(AnalyzerProviderAttribute));
                    if (attr != null && attr.ProviderType != null)
                    {
                        // Create an instance of the provider.
                        IAnalyzerProvider analyzerProvider = Activator.CreateInstance(attr.ProviderType) as IAnalyzerProvider;
                        if (analyzerProvider != null)
                        {
                            foreach (Analyzer analyzer in analyzerProvider.GetAnalyzers())
                            {
                                yield return(analyzer);
                            }
                        }
                    }
                }
            }
#else
            yield break;
#endif
        }