container icon indicating copy to clipboard operation
container copied to clipboard

DependencyAttribute is ignored when injecting by name

Open ENikS opened this issue 6 years ago • 0 comments

Description

A clear and concise description of what is wrong.

To Reproduce

Please provide UnitTest in the form of:

        public class ObjectWithAttributes
        {
            [Dependency("name1")]
            public string Dependency;
        }

Container.RegisterInstance(Name);
Container.RegisterInstance(Name, Name);
Container.RegisterInstance(Name1, Name1);


[TestMethod]
public void ResolveOverAttributeAny()
{
    // Setup
    Container.RegisterType<ObjectWithAttributes>(
        Resolve.Field(nameof(ObjectWithAttributes.Dependency)));

    // Act
    var result = Container.Resolve<ObjectWithAttributes>();

    // Verify
    Assert.IsNotNull(result);
    Assert.IsNotNull(result.Dependency);
    Assert.AreEqual(result.Dependency, Name1);
    Assert.IsNull(result.Optional);
}


[TestMethod]
public void ResolveOverAttribute()
{
    // Setup
    Container.RegisterType<ObjectWithAttributes>(
        new InjectionField(nameof(ObjectWithAttributes.Dependency), (string)null));

    // Act
    var result = Container.Resolve<ObjectWithAttributes>();

    // Verify
    Assert.IsNotNull(result);
    Assert.IsNotNull(result.Dependency);
    Assert.AreEqual(result.Dependency, Name);
    Assert.IsNull(result.Optional);
}

Additional context

Add any other context about the problem here.

ENikS avatar Nov 06 '20 00:11 ENikS