|
8 | 8 | using Unity.Exceptions; |
9 | 9 | using Unity.Policy; |
10 | 10 | using Unity.Resolution; |
| 11 | +using Unity.Registration; |
| 12 | +using Unity.Injection; |
11 | 13 |
|
12 | 14 | namespace Unity.Processors |
13 | 15 | { |
@@ -73,6 +75,79 @@ public ConstructorDiagnostic(IPolicySet policySet, Func<Type, bool> isTypeRegist |
73 | 75 | #endregion |
74 | 76 |
|
75 | 77 |
|
| 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 | + |
76 | 151 | #region Expression Overrides |
77 | 152 |
|
78 | 153 | public override IEnumerable<Expression> GetExpressions(Type type, IPolicySet registration) |
|
0 commit comments