Skip to content

Commit

Permalink
Improve discovery of executable methods to include implemented interf…
Browse files Browse the repository at this point in the history
…aces
  • Loading branch information
almostchristian committed Oct 11, 2022
1 parent 2a383a7 commit 32536d6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/ConfigurationProcessor.Core/Implementation/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void CallConfigurationMethods(
IEnumerable? GetCollection(MethodInfo method)
{
var argValue = new ObjectArgumentValue(configSection!);
var collectionType = method.GetParameters().ElementAt(1).ParameterType;
var collectionType = method.GetParameters().ElementAt(method.IsStatic ? 1 : 0).ParameterType;
return argValue.ConvertTo(method, collectionType, resolutionContext) as ICollection;
}

Expand Down Expand Up @@ -185,15 +185,16 @@ private static List<MethodInfo> FindConfigurationExtensionMethods(
MethodFilter? filter)
{
IReadOnlyCollection<Assembly> configurationAssemblies = resolutionContext.ConfigurationAssemblies;

var interfaces = configType.GetInterfaces();
var candidateMethods = configurationAssemblies
.SelectMany(a => SafeGetExportedTypes(a)
.Select(t => t.GetTypeInfo())
.Where(t => t.IsSealed && t.IsAbstract && !t.IsNested))
.Union(new[] { configType.GetTypeInfo() })
.Concat(interfaces.Select(t => t.GetTypeInfo()))
.SelectMany(t => candidateNames != null ? candidateNames.SelectMany(n => t.GetDeclaredMethods(n)) : t.DeclaredMethods)
.Where(m => filter == null || filter(m, key))
.Where(m => !m.IsDefined(typeof(CompilerGeneratedAttribute), false) && m.IsPublic && ((m.IsStatic && m.IsDefined(typeof(ExtensionAttribute), false)) || m.DeclaringType == configType))
.Where(m => !m.IsDefined(typeof(CompilerGeneratedAttribute), false) && m.IsPublic && ((m.IsStatic && m.IsDefined(typeof(ExtensionAttribute), false)) || m.DeclaringType == configType || interfaces.Contains(m.DeclaringType)))
.Where(m => !m.IsStatic || SafeGetParameters(m).ElementAtOrDefault(0)?.ParameterType.IsAssignableFrom(configType) == true) // If static method, checks that the first parameter is same as the extension type
.ToList();

Expand Down
4 changes: 3 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.3.0</Version>
<Version>1.4.0</Version>
<FileVersion>$(Version).$([System.DateTime]::Now.ToString(yy))$([System.DateTime]::Now.DayOfYear.ToString(000))</FileVersion>
<PackageVersion>$(Version)</PackageVersion>
<InformationalVersion>$(FileVersion)-$(GIT_VERSION)</InformationalVersion>
Expand All @@ -23,6 +23,8 @@
<PackageTags>dependencyinjection;configuration;ioc;di;</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
v1.4.0
- Improve discovery of executable methods to include implemented interfaces
v1.3.0
- Add support for disambiguating overloads with different array types.
- Prefer overloads with the most matching parameters
Expand Down
5 changes: 1 addition & 4 deletions tests/TestDummies/IChildValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

namespace TestDummies
{
public interface IChildValue
public interface IChildValue : IRootChildValue
{
string Child { get; set; }
Type ContextType { get; set; }
Uri Location { get; set; }
Delegate OnError { get; set; }
TimeSpan? Time { get; set; }

void Reset();
}
}
13 changes: 13 additions & 0 deletions tests/TestDummies/IRootChildValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Integrated Health Information Systems Pte Ltd. All rights reserved.
// -------------------------------------------------------------------------------------------------

namespace TestDummies
{
public interface IRootChildValue
{
string Child { get; set; }

void Reset();
}
}

0 comments on commit 32536d6

Please sign in to comment.