Skip to content

Commit e65799e

Browse files
committed
Fixing InjectionField and InjectionProperty with Type resolution
Fixed #292
1 parent 75c3e38 commit e65799e

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

package.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionBase>5.11.8</VersionBase>
4+
<VersionBase>5.11.9</VersionBase>
55
<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>
66
</PropertyGroup>
77

src/Builder/Context/BuilderContext.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ public object Resolve(PropertyInfo property, object value)
257257
// Resolve from injectors
258258
switch (value)
259259
{
260+
case PropertyInfo info when
261+
ReferenceEquals(info, property):
262+
return Resolve(info.PropertyType, null);
263+
260264
case DependencyAttribute dependencyAttribute:
261265
return Resolve(property.PropertyType, dependencyAttribute.Name);
262266

@@ -312,6 +316,10 @@ public object Resolve(FieldInfo field, object value)
312316
// Resolve from injectors
313317
switch (value)
314318
{
319+
case FieldInfo info when
320+
ReferenceEquals(info, field):
321+
return Resolve(info.FieldType, null);
322+
315323
case DependencyAttribute dependencyAttribute:
316324
return Resolve(field.FieldType, dependencyAttribute.Name);
317325

tests/Unity.Specification/Parameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public override IUnityContainer GetContainer()
1313
}
1414

1515
[TestClass]
16-
public class Injected : Unity.Specification.Parameter.Injection.SpecificationTests
16+
public class Injected : Unity.Specification.Parameter.Injected.SpecificationTests
1717
{
1818
public override IUnityContainer GetContainer()
1919
{
@@ -63,7 +63,7 @@ public override IUnityContainer GetContainer()
6363
}
6464

6565
[TestClass]
66-
public class Injected : Unity.Specification.Parameter.Injection.SpecificationTests
66+
public class Injected : Unity.Specification.Parameter.Injected.SpecificationTests
6767
{
6868
public override IUnityContainer GetContainer()
6969
{

tests/Unity.Tests/Injection/OptionalDependencyFixture.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ namespace Unity.Tests.v5.Injection
1010
[TestClass]
1111
public class OptionalDependencyFixture
1212
{
13+
14+
public class ObjectWithProperty
15+
{
16+
public string Property { get; set; }
17+
}
18+
19+
20+
[TestMethod]
21+
// https://github.com/unitycontainer/container/issues/292
22+
public void ByType()
23+
{
24+
// Setup
25+
IUnityContainer Container = new UnityContainer()
26+
.RegisterInstance("name");
27+
28+
Container.RegisterType<ObjectWithProperty>(
29+
new InjectionProperty(nameof(ObjectWithProperty.Property), typeof(string)));
30+
31+
// Act
32+
var result = Container.Resolve<ObjectWithProperty>();
33+
34+
// Verify
35+
Assert.IsNotNull(result);
36+
Assert.IsNotNull(result.Property);
37+
Assert.IsInstanceOfType(result.Property, typeof(string));
38+
}
39+
40+
1341
[TestMethod]
1442
public void OptionalParametersSetToNullIfNotRegistered()
1543
{

0 commit comments

Comments
 (0)