From 3b804aa713f4f7be3774951f9a74bb6f2e1e2282 Mon Sep 17 00:00:00 2001 From: almostchristian Date: Sat, 22 Oct 2022 16:47:09 +0800 Subject: [PATCH] Fixed exception with optional parameters --- .../Implementation/Extensions.cs | 20 +++++++++++++------ src/Directory.Build.props | 4 +++- .../DummyServiceCollectionExtensions.cs | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ConfigurationProcessor.Core/Implementation/Extensions.cs b/src/ConfigurationProcessor.Core/Implementation/Extensions.cs index 386182d..66a116a 100644 --- a/src/ConfigurationProcessor.Core/Implementation/Extensions.cs +++ b/src/ConfigurationProcessor.Core/Implementation/Extensions.cs @@ -169,12 +169,20 @@ public static void CallConfigurationMethods( } else { - var call = (from p in configurationMethod.GetParameters().Skip(configurationMethod.IsStatic ? 1 : 0) - let directive = paramArgs.FirstOrDefault>(s => string.IsNullOrEmpty(s.Key) || ParameterNameMatches(p.Name!, s.Key)) - select directive.Key == null - ? resolutionContext.GetImplicitValueForNotSpecifiedKey(p, configurationMethod, paramArgs.FirstOrDefault().Value.ConfigSection, methodName) - : directive.Value.ArgName.ConvertTo(configurationMethod, p.ParameterType, resolutionContext)).ToList(); - invoker(call, configurationMethod); + var parameters = configurationMethod.GetParameters().Skip(configurationMethod.IsStatic ? 1 : 0).ToArray(); + List args = new List(); + + for (int i = 0; i < parameters.Length; i++) + { + var p = parameters[i]; + var directive = paramArgs.FirstOrDefault(s => string.IsNullOrEmpty(s.Key) || ParameterNameMatches(p.Name!, s.Key)); + var arg = (directive.Key == null || (string.IsNullOrEmpty(directive.Key) && i > 0)) ? + resolutionContext.GetImplicitValueForNotSpecifiedKey(p, configurationMethod, paramArgs.FirstOrDefault().Value.ConfigSection, methodName)! : + directive.Value.ArgName.ConvertTo(configurationMethod, p.ParameterType, resolutionContext)!; + args.Add(arg); + } + + invoker(args, configurationMethod); } } else diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 4d4b01e..fcecc10 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -5,7 +5,7 @@ - 1.5.0 + 1.5.1 $(Version).$([System.DateTime]::Now.ToString(yy))$([System.DateTime]::Now.DayOfYear.ToString(000)) $(Version) $(FileVersion)-$(GIT_VERSION) @@ -23,6 +23,8 @@ dependencyinjection;configuration;ioc;di; README.md +v1.5.1 + - Fixed exception with optional parameters v1.5.0 - Improve selection of single parameter overload method. - Improve binding to readonly properties diff --git a/tests/TestDummies/DummyServiceCollectionExtensions.cs b/tests/TestDummies/DummyServiceCollectionExtensions.cs index 1f67829..7a0c1d2 100644 --- a/tests/TestDummies/DummyServiceCollectionExtensions.cs +++ b/tests/TestDummies/DummyServiceCollectionExtensions.cs @@ -61,7 +61,7 @@ public static IServiceCollection AddSimpleString(this IServiceCollection service return services; } - public static IServiceCollection AddSimpleInt32(this IServiceCollection services, int value) + public static IServiceCollection AddSimpleInt32(this IServiceCollection services, int value, bool rev = false) { services.AddSingleton(new SimpleValue(value)); return services;