Skip to content

ConfigurationBinder.Bind on virtual array property duplicates elements #70150

@ghost

Description

Description

Microsoft.Extensions.Configuration.ConfigurationBinder.Bind sets virtual properties multiple times when overridden. This appends a duplicate array for each overridden instance of virtual array properties.

This appears to come from ConfigurationBinder.GetAllProperties, it adds overridden properties to its returned list multiple times.

Reproduction Steps

public class C1
{
    public virtual string[] Test { get; set; } = System.Array.Empty<string>();
}

public class C2 : C1
{
    public override string[] Test { get => base.Test; set => base.Test = value; }
}

var configuration = new ConfigurationBuilder()
    .AddInMemoryCollection(new Dictionary<string, string>()
    {
        ["Test:0"] = "1",
    })
    .Build();

var test = new C2();
configuration.Bind(test);
// test.Test has two elements with value of "1"

Expected behavior

I would expect test.Test to have one element "1"

Actual behavior

test.Test has two elements with value of "1"

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions