Skip to content

Commit

Permalink
Fixing InjectionField and InjectionProperty with Type resolution
Browse files Browse the repository at this point in the history
Fixed #292
  • Loading branch information
ENikS committed Nov 23, 2020
1 parent 75c3e38 commit e65799e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionBase>5.11.8</VersionBase>
<VersionBase>5.11.9</VersionBase>
<PackageReleaseNotes>This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7</PackageReleaseNotes>
</PropertyGroup>

Expand Down
8 changes: 8 additions & 0 deletions src/Builder/Context/BuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ public object Resolve(PropertyInfo property, object value)
// Resolve from injectors
switch (value)
{
case PropertyInfo info when
ReferenceEquals(info, property):
return Resolve(info.PropertyType, null);

case DependencyAttribute dependencyAttribute:
return Resolve(property.PropertyType, dependencyAttribute.Name);

Expand Down Expand Up @@ -312,6 +316,10 @@ public object Resolve(FieldInfo field, object value)
// Resolve from injectors
switch (value)
{
case FieldInfo info when
ReferenceEquals(info, field):
return Resolve(info.FieldType, null);

case DependencyAttribute dependencyAttribute:
return Resolve(field.FieldType, dependencyAttribute.Name);

Expand Down
4 changes: 2 additions & 2 deletions tests/Unity.Specification/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override IUnityContainer GetContainer()
}

[TestClass]
public class Injected : Unity.Specification.Parameter.Injection.SpecificationTests
public class Injected : Unity.Specification.Parameter.Injected.SpecificationTests
{
public override IUnityContainer GetContainer()
{
Expand Down Expand Up @@ -63,7 +63,7 @@ public override IUnityContainer GetContainer()
}

[TestClass]
public class Injected : Unity.Specification.Parameter.Injection.SpecificationTests
public class Injected : Unity.Specification.Parameter.Injected.SpecificationTests
{
public override IUnityContainer GetContainer()
{
Expand Down
28 changes: 28 additions & 0 deletions tests/Unity.Tests/Injection/OptionalDependencyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ namespace Unity.Tests.v5.Injection
[TestClass]
public class OptionalDependencyFixture
{

public class ObjectWithProperty
{
public string Property { get; set; }
}


[TestMethod]
// https://github.com/unitycontainer/container/issues/292
public void ByType()
{
// Setup
IUnityContainer Container = new UnityContainer()
.RegisterInstance("name");

Container.RegisterType<ObjectWithProperty>(
new InjectionProperty(nameof(ObjectWithProperty.Property), typeof(string)));

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

// Verify
Assert.IsNotNull(result);
Assert.IsNotNull(result.Property);
Assert.IsInstanceOfType(result.Property, typeof(string));
}


[TestMethod]
public void OptionalParametersSetToNullIfNotRegistered()
{
Expand Down

0 comments on commit e65799e

Please sign in to comment.