Skip to content

Config generator wrongly emits init-related exception in bind operations #90974

Closed
@stephentoub

Description

@stephentoub

This code without the generator:

using Microsoft.Extensions.Configuration;

Environment.SetEnvironmentVariable("Value", "42");
IConfiguration b = new ConfigurationBuilder().AddEnvironmentVariables().Build();

Base d = new Derived();
b.Bind(d);
Console.WriteLine(d.Value);

internal abstract class Base
{
    public int Value { get; set; }
}

internal sealed class Derived : Base { }

prints 42.

With the config generator, that same code throws an exception, because the generator emits this:

public static void Bind_Base(this IConfiguration configuration, object? obj)
{
    throw new InvalidOperationException("Cannot create instance of type '<global namespace>.Base' because it is missing a public instance constructor.");
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions