private static bool TryGetModules(AssemblyMetadata assemblyMetadata, out ImmutableArray <ModuleMetadata> modules) { // Gracefully handle documented exceptions from 'GetModules' invocation. try { modules = assemblyMetadata.GetModules(); return(true); } catch (Exception ex) when(ex is BadImageFormatException || ex is IOException || ex is ObjectDisposedException) { modules = default; return(false); } }
/// <summary> /// Opens the analyzer dll with the metadata reader and builds a map of language -> analyzer type names. /// </summary> /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception> /// <exception cref="IOException">IO error reading the metadata.</exception> private static ImmutableDictionary <string, ImmutableHashSet <string> > GetAnalyzerTypeNameMap(string fullPath, AttributePredicate attributePredicate) { using (AssemblyMetadata assembly = AssemblyMetadata.CreateFromFile(fullPath)) { IEnumerable <IGrouping <string, string> > typeNameMap = from module in assembly.GetModules() from typeDefHandle in module.MetadataReader.TypeDefinitions let typeDef = module.MetadataReader.GetTypeDefinition(typeDefHandle) let typeName = GetFullyQualifiedTypeName(typeDef, module.Module) from supportedLanguage in GetSupportedLanguages(typeDef, module.Module, attributePredicate) group typeName by supportedLanguage; return(typeNameMap.ToImmutableDictionary(g => g.Key, g => g.ToImmutableHashSet())); } }