Skip to content

Possible regression: ConfigurationBinder cannot bind private properties of base classes #71040

@lateapexearlyspeed

Description

@lateapexearlyspeed

Description

In recent main branch, I found seems ConfigurationBinder cannot bind private properties of base classes, but before that, it can (for example .net 3, 5, 6).

In .net 3, 5, 6:
image

Recent main branch (I add an test case to show result):
image

        [Fact]
        public void CanBindPrivatePropertiesFromBaseClass()
        {
            ConfigurationBuilder configurationBuilder = new();
            configurationBuilder.AddInMemoryCollection(new Dictionary<string, string>
            {
                { "PrivateProperty", "a" }
            });
            IConfiguration config = configurationBuilder.Build();

            var test = new ClassOverridingVirtualProperty();
            config.Bind(test, b => b.BindNonPublicProperties = true);
            Assert.Equal("a", test.ExposePrivatePropertyValue());
        }

        public class BaseClassWithVirtualProperty
        {
            private string? PrivateProperty { get; set; }

            public string? ExposePrivatePropertyValue() => PrivateProperty;
        }

        public class ClassOverridingVirtualProperty : BaseClassWithVirtualProperty
        {
        }

I raised PR to try to fix that, meantime can handle virtual properties.

Reproduction Steps

As above.

Expected behavior

ConfigurationBinder may should be able to bind private properties of base classes.

Actual behavior

ConfigurationBinder cannot bind private properties of base classes in recent main branch.

Regression?

Yes, it did.

Known Workarounds

No response

Configuration

Found this from main branch with commit version: 2ce8130, at least.

Other information

I raised PR to try to fix that, meantime can handle virtual properties.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions