Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensions method discovery workaround #333

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -548,15 +549,24 @@ static IReadOnlyCollection<MethodInfo> FindEventEnricherConfigurationMethods(IRe
static List<MethodInfo> FindConfigurationExtensionMethods(IReadOnlyCollection<Assembly> configurationAssemblies, Type configType)
{
// ExtensionAttribute can be polyfilled to support extension methods
bool HasExtensionAttribute(MethodInfo m) =>
m.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute");
static bool HasCustomExtensionAttribute(MethodInfo m)
{
try
{
return m.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute");
}
catch (CustomAttributeFormatException)
{
return false;
}
}

return configurationAssemblies
.SelectMany(a => a.ExportedTypes
.Select(t => t.GetTypeInfo())
.Where(t => t.IsSealed && t.IsAbstract && !t.IsNested))
.SelectMany(t => t.DeclaredMethods)
.Where(m => m.IsStatic && m.IsPublic && HasExtensionAttribute(m))
.Where(m => m.IsStatic && m.IsPublic && (m.IsDefined(typeof(ExtensionAttribute), false) || HasCustomExtensionAttribute(m)))
.Where(m => m.GetParameters()[0].ParameterType == configType)
.ToList();
}
Expand Down