Skip to content

Commit

Permalink
Fixed exception with optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
almostchristian committed Oct 22, 2022
1 parent 71c8c90 commit 3b804aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/ConfigurationProcessor.Core/Implementation/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyValuePair<string, (IConfigurationArgumentValue ArgName, IConfigurationSection ConfigSection)>>(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<object>();
invoker(call, configurationMethod);
var parameters = configurationMethod.GetParameters().Skip(configurationMethod.IsStatic ? 1 : 0).ToArray();
List<object> args = new List<object>();

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
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.5.0</Version>
<Version>1.5.1</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.5.1
- Fixed exception with optional parameters
v1.5.0
- Improve selection of single parameter overload method.
- Improve binding to readonly properties
Expand Down
2 changes: 1 addition & 1 deletion tests/TestDummies/DummyServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(value));
return services;
Expand Down

0 comments on commit 3b804aa

Please sign in to comment.