Description
When binding to a type with a constructor that has a default string parameter, the generator emits code that doesn't compile.
C:\scratch\configStringCtorParam\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Config
uration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(77,27): error CS0103: The name 's'
does not exist in the current context [C:\scratch\configStringCtorParam\configStringCtorParam.csproj]
The code generated looks like this:
public static SD InitializeSD(IConfiguration configuration, BinderOptions? binderOptions)
{
if (configuration["S"] is string value1)
{
string s = value1;
}
return new SD(s);
}
Bug is likely in the EmitBindImplForMember method but I haven't tracked it down. I suspect there's a disagreement between the case where the member is string parseable but also has a default value. This same repro works when removing the default value.
Reproduction Steps
using Microsoft.Extensions.Configuration;
var c = new ConfigurationBuilder().Build();
var x = c.Get<SD>();
Console.WriteLine(x);
public class SD
{
public SD(string s = "default value") { S = s; }
public string S { get;}
public override string ToString() => S;
}
Expected behavior
Compiles successfully.
Actual behavior
Compilation failure.
Regression?
No
Known Workarounds
Remove default value from ctor param.
Configuration
No response
Other information
No response
Description
When binding to a type with a constructor that has a default string parameter, the generator emits code that doesn't compile.
The code generated looks like this:
Bug is likely in the EmitBindImplForMember method but I haven't tracked it down. I suspect there's a disagreement between the case where the member is string parseable but also has a default value. This same repro works when removing the default value.
Reproduction Steps
Expected behavior
Compiles successfully.
Actual behavior
Compilation failure.
Regression?
No
Known Workarounds
Remove default value from ctor param.
Configuration
No response
Other information
No response