protected override string GetAnalyzerAssemblyPath(AnalyzerFileReference reference) { // TODO: find out a way to get analyzer assembly location // without actually loading analyzer in memory var assembly = reference.GetAssembly(); return assembly?.Location; }
internal void AddExtensions(ImmutableDictionary <string, ImmutableArray <TExtension> > .Builder builder) { ImmutableDictionary <string, ImmutableHashSet <string> > analyzerTypeNameMap; Assembly analyzerAssembly; try { analyzerTypeNameMap = GetExtensionTypeNameMap(); if (analyzerTypeNameMap.Count == 0) { return; } analyzerAssembly = _reference.GetAssembly(); } catch (Exception e) { _reference.AnalyzerLoadFailed?.Invoke(_reference, CreateAnalyzerFailedArgs(e)); return; } int initialCount = builder.Count; bool reportedError = false; // Add language specific analyzers. foreach (string language in analyzerTypeNameMap.Keys) { if (language == null) { continue; } ImmutableArray <TExtension> analyzers = GetLanguageSpecificAnalyzers(analyzerAssembly, analyzerTypeNameMap, language, ref reportedError); builder.Add(language, analyzers); } // If there were types with the attribute but weren't an analyzer, generate a diagnostic. // If we've reported errors already while trying to instantiate types, don't complain that there are no analyzers. if (builder.Count == initialCount && !reportedError) { _reference.AnalyzerLoadFailed?.Invoke(_reference, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, CodeAnalysisResources.NoAnalyzersFound)); } }
public void AssemblyLoading() { StringBuilder sb = new StringBuilder(); var directory = Temp.CreateDirectory(); EventHandler<AnalyzerAssemblyLoadEventArgs> handler = (e, args) => { var relativePath = args.Path.Substring(directory.Path.Length); sb.AppendFormat("Assembly {0} loaded from {1}", args.LoadedAssembly.FullName, relativePath); sb.AppendLine(); }; AnalyzerFileReference.AssemblyLoad += handler; var alphaDll = directory.CreateFile("Alpha.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Alpha); var betaDll = directory.CreateFile("Beta.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Beta); var gammaDll = directory.CreateFile("Gamma.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Gamma); var deltaDll = directory.CreateFile("Delta.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Delta); AnalyzerFileReference alphaReference = new AnalyzerFileReference(alphaDll.Path); Assembly alpha = alphaReference.GetAssembly(); File.Delete(alphaDll.Path); var a = alpha.CreateInstance("Alpha.A"); a.GetType().GetMethod("Write").Invoke(a, new object[] { sb, "Test A" }); File.Delete(gammaDll.Path); File.Delete(deltaDll.Path); AnalyzerFileReference betaReference = new AnalyzerFileReference(betaDll.Path); Assembly beta = betaReference.GetAssembly(); var b = beta.CreateInstance("Beta.B"); b.GetType().GetMethod("Write").Invoke(b, new object[] { sb, "Test B" }); var expected = @"Assembly Alpha, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null loaded from \Alpha.dll Assembly Gamma, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null loaded from \Gamma.dll Assembly Delta, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null loaded from \Delta.dll Delta: Gamma: Alpha: Test A Assembly Beta, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null loaded from \Beta.dll Delta: Gamma: Beta: Test B "; var actual = sb.ToString(); Assert.Equal(expected, actual); AnalyzerFileReference.AssemblyLoad -= handler; }