- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Closed
Labels
area-Extensions-Configurationsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature
Description
In a project that I am working on, we have hierarchical configuration. It means that the parent and child config sections can provide same properties.
Following repro shows the issue:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace ConfigBindRepro
{
    internal class Program
    {
        static void Main()
        {
            HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
            builder.Configuration.AddInMemoryCollection(new KeyValuePair<string, string?>[]
            {
                 new("A:B:ConnectionString", "localhost"),
            });
            AClass instance = new();
            builder.Configuration.GetSection("A:B").Bind(instance); // sets ConnectionString to "localhost"
            builder.Configuration.GetSection("A").Bind(instance); // source gen: sets ConnectionString to null, reflection: does not change the value
            Console.WriteLine($"The value is '{instance.ConnectionString}'");
        }
    }
    public class AClass
    {
        public string? ConnectionString { get; set; }
    }
}When using reflection based binder:
The value is 'localhost'
When using source gen:
The value is ''
Metadata
Metadata
Assignees
Labels
area-Extensions-Configurationsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature