Skip to content

Private properties on base class should be probed and exception thrown when marked for injection #893

@dotnetjunkie

Description

@dotnetjunkie

Code to demonstrate the problem:

public abstract class BaseComponent
{
    [Dependency] private ILogger logger { get; set; }
    public bool HasLogger => this.logger != null;
}

public class InheritedComponent : BaseComponent { }

public sealed class DependencyAttribute : Attribute { }

class DependencyAttributePropertySelectionBehavior : IPropertySelectionBehavior
{
    public bool SelectProperty(Type type, PropertyInfo prop) =>
        prop.GetCustomAttributes(typeof(DependencyAttribute)).Any();
}

[TestMethod]
public void Ctor_Always_Succeeds()
{
    // Act
    var container = new Container();
    container.Options.PropertySelectionBehavior = new DependencyAttributePropertySelectionBehavior();
    container.Register<InheritedComponent>();
    container.Register<ILogger, NullLogger>();

    Assert.IsTrue(container.GetInstance<InheritedComponent>().HasLogger);
}

The test fails, while the expected behavior is for the property to get injected, because:

  • Private properties on the derived class are injected
  • This worked in the past and is likely a regression that was introduced in v4.

Fixing this issue, however, should be considered a breaking change, because Simple Injector will start calling SelectProperty on properties that were previously skipped.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions