Skip to content

Commit

Permalink
Merge pull request #333 from skomis-mm/extMethodDiscoveryFix
Browse files Browse the repository at this point in the history
Extensions method discovery workaround
  • Loading branch information
skomis-mm authored Oct 13, 2022
2 parents 8f241f8 + 8391832 commit e171429
Showing 1 changed file with 13 additions and 3 deletions.
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

0 comments on commit e171429

Please sign in to comment.