Skip to content

Commit

Permalink
Merge branch 'master' into update-roslyn-3
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich authored Jul 26, 2024
2 parents ce58118 + 50095f2 commit 2efe02c
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,30 @@ private ImmutableArray<CodeFixProvider> LoadFrom(Project project)
{
var codeFixesFromProjectReferences = project.AnalyzerReferences
.OfType<AnalyzerFileReference>()
.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<ExportCodeFixProviderAttribute>();
var attribute = x.GetCustomAttribute<ExportCodeFixProviderAttribute>(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<CodeFixProvider>();
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;
}
})
Expand All @@ -98,5 +98,25 @@ private ImmutableArray<CodeFixProvider> LoadFrom(Project project)

return allCodeFixes;
}

private IEnumerable<Type> 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;
}
}
}
}

0 comments on commit 2efe02c

Please sign in to comment.