From 57f589840b5bbc57aced8347ee6b0d0f04b454e5 Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Thu, 25 Jul 2024 16:40:41 -0700 Subject: [PATCH] Fix code fix construction --- .../V2/CachingCodeFixProviderForProjects.cs | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/CachingCodeFixProviderForProjects.cs b/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/CachingCodeFixProviderForProjects.cs index ea230f0a16..f5ad4df1cc 100644 --- a/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/CachingCodeFixProviderForProjects.cs +++ b/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/CachingCodeFixProviderForProjects.cs @@ -61,30 +61,30 @@ private ImmutableArray LoadFrom(Project project) { var codeFixesFromProjectReferences = project.AnalyzerReferences .OfType() - .SelectMany(analyzerFileReference => analyzerFileReference.GetAssembly().DefinedTypes) + .SelectMany(analyzerFileReference => GetConcreteTypes(analyzerFileReference.GetAssembly())) .Where(x => !x.IsAbstract && x.IsSubclassOf(typeof(CodeFixProvider))) .Select(x => { try { - var attribute = x.GetCustomAttribute(); + var attribute = x.GetCustomAttribute(inherit: false); if (attribute == null) { - _logger.LogTrace($"Skipping code fix provider '{x.AsType()}' because it is missing the ExportCodeFixProviderAttribute."); + _logger.LogTrace($"Skipping code fix provider '{x}' because it is missing the ExportCodeFixProviderAttribute."); return null; } if (attribute.Languages == null || !attribute.Languages.Contains(project.Language)) { - _logger.LogInformation($"Skipping code fix provider '{x.AsType()}' because its language '{attribute.Languages?.FirstOrDefault()}' doesn't match '{project.Language}'."); + _logger.LogInformation($"Skipping code fix provider '{x}' because its language '{attribute.Languages?.FirstOrDefault()}' doesn't match '{project.Language}'."); return null; } - return x.AsType().CreateInstance(); + return (CodeFixProvider)Activator.CreateInstance(x); } catch (Exception ex) { - _logger.LogError($"Creating instance of code fix provider '{x.AsType()}' failed, error: {ex}"); + _logger.LogError($"Creating instance of code fix provider '{x}' failed, error: {ex}"); return null; } }) @@ -98,5 +98,25 @@ private ImmutableArray LoadFrom(Project project) return allCodeFixes; } + + private IEnumerable GetConcreteTypes(Assembly assembly) + { + try + { + var concreteTypes = assembly + .GetTypes() + .Where(type => !type.GetTypeInfo().IsInterface + && !type.GetTypeInfo().IsAbstract + && !type.GetTypeInfo().ContainsGenericParameters); + + // Realize the collection to ensure exceptions are caught + return concreteTypes.ToList(); + } + catch (Exception ex) + { + _logger.LogError($"Getting concrete types from assembly '{assembly}' failed, error: {ex}"); + return Type.EmptyTypes; + } + } } }