Skip to content

Commit 27b61fa

Browse files
committed
Improving constructor Diagnostic
1 parent 7f601e5 commit 27b61fa

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

src/Processors/Constructor/ConstructorDiagnostic.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Unity.Exceptions;
99
using Unity.Policy;
1010
using Unity.Resolution;
11+
using Unity.Registration;
12+
using Unity.Injection;
1113

1214
namespace Unity.Processors
1315
{
@@ -73,6 +75,79 @@ public ConstructorDiagnostic(IPolicySet policySet, Func<Type, bool> isTypeRegist
7375
#endregion
7476

7577

78+
#region Selection
79+
80+
public override IEnumerable<object> Select(Type type, IPolicySet registration)
81+
{
82+
var members = new List<InjectionMember>();
83+
84+
// Select Injected Members
85+
if (null != ((InternalRegistration)registration).InjectionMembers)
86+
{
87+
foreach (var injectionMember in ((InternalRegistration)registration).InjectionMembers)
88+
{
89+
if (injectionMember is InjectionMember<ConstructorInfo, object[]>)
90+
{
91+
members.Add(injectionMember);
92+
}
93+
}
94+
}
95+
96+
switch (members.Count)
97+
{
98+
case 1:
99+
return members.ToArray();
100+
101+
case 0:
102+
break;
103+
104+
default:
105+
throw new InvalidOperationException($"Multiple Injection Constructors are registered for Type {type.FullName}");
106+
}
107+
108+
// Enumerate to array
109+
var constructors = DeclaredMembers(type).ToArray();
110+
if (1 >= constructors.Length)
111+
return constructors;
112+
113+
var selection = new HashSet<ConstructorInfo>();
114+
115+
// Select Attributed constructors
116+
foreach (var constructor in constructors)
117+
{
118+
for (var i = 0; i < AttributeFactories.Length; i++)
119+
{
120+
#if NET40
121+
if (!constructor.IsDefined(AttributeFactories[i].Type, true))
122+
#else
123+
if (!constructor.IsDefined(AttributeFactories[i].Type))
124+
#endif
125+
continue;
126+
127+
selection.Add(constructor);
128+
}
129+
}
130+
131+
switch (selection.Count)
132+
{
133+
case 1:
134+
return selection.ToArray();
135+
136+
case 0:
137+
break;
138+
139+
default:
140+
throw new InvalidOperationException($"Multiple Constructors are annotated for injection on Type {type.FullName}");
141+
}
142+
143+
// Select default
144+
return new[] { SelectMethod(type, constructors) };
145+
}
146+
147+
148+
#endregion
149+
150+
76151
#region Expression Overrides
77152

78153
public override IEnumerable<Expression> GetExpressions(Type type, IPolicySet registration)

tests/Unity.Diagnostic/Constructor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
namespace Compiled.Constructor
66
{
7+
[TestClass]
8+
public class Annotation : Unity.Specification.Diagnostic.Constructor.Annotation.SpecificationTests
9+
{
10+
public override IUnityContainer GetContainer()
11+
{
12+
return new UnityContainer(UnityContainer.BuildStrategy.Compiled)
13+
.AddExtension(new Diagnostic());
14+
}
15+
}
16+
717
[TestClass]
818
public class Parameters : Unity.Specification.Diagnostic.Constructor.Parameters.SpecificationTests
919
{
@@ -37,6 +47,16 @@ public override IUnityContainer GetContainer()
3747

3848
namespace Resolved.Constructor
3949
{
50+
[TestClass]
51+
public class Annotation : Unity.Specification.Diagnostic.Constructor.Annotation.SpecificationTests
52+
{
53+
public override IUnityContainer GetContainer()
54+
{
55+
return new UnityContainer(UnityContainer.BuildStrategy.Compiled)
56+
.AddExtension(new Diagnostic());
57+
}
58+
}
59+
4060
[TestClass]
4161
public class Parameters : Unity.Specification.Diagnostic.Constructor.Parameters.SpecificationTests
4262
{

0 commit comments

Comments
 (0)