@@ -36,9 +36,9 @@ internal void ValidateService(ServiceDescriptor descriptor)
3636 /// </summary>
3737 internal void DisposeServices ( )
3838 {
39- for ( int i = Services . Count - 1 ; i >= 0 ; i -- )
39+ for ( int index = Services . Count - 1 ; index >= 0 ; index -- )
4040 {
41- if ( Services [ i ] . ImplementationInstance is IDisposable disposable )
41+ if ( Services [ index ] . ImplementationInstance is IDisposable disposable )
4242 {
4343 disposable . Dispose ( ) ;
4444 }
@@ -160,7 +160,8 @@ private object Resolve(Type implementationType)
160160
161161 if ( constructorParameters == null )
162162 {
163- throw new InvalidOperationException ( ) ;
163+ throw new InvalidOperationException (
164+ $ "Constructor for '{ implementationType } ' could not be located.") ;
164165 }
165166
166167 if ( constructorParameters . Length == 0 )
@@ -176,18 +177,19 @@ private object Resolve(Type implementationType)
176177 {
177178 var parameterType = constructorParameters [ index ] . ParameterType ;
178179
179- if ( TryBindToPrimitive ( parameterType , out object defaultType ) )
180+ if ( parameterType . IsResolvable ( ) )
180181 {
181182 types [ index ] = parameterType ;
182- parameters [ index ] = defaultType ;
183+ parameters [ index ] = GetResolvableDefault ( parameterType ) ;
183184 }
184185 else
185186 {
186187 var service = GetService ( parameterType ) ;
187188
188189 if ( service == null )
189190 {
190- throw new InvalidOperationException ( $ "'{ implementationType } '->'{ parameterType } '.") ;
191+ throw new InvalidOperationException (
192+ $ "Unable to resolve service for '{ parameterType } '.") ;
191193 }
192194
193195 types [ index ] = parameterType ;
@@ -220,18 +222,34 @@ private ParameterInfo[] GetParameters(Type implementationType)
220222 {
221223 if ( constructors [ j ] . GetParameters ( ) . Length == constructors [ j + 1 ] . GetParameters ( ) . Length )
222224 {
223- throw new InvalidOperationException ( ) ;
225+ throw new InvalidOperationException (
226+ $ "Multiple constructors found in '{ implementationType } '.") ;
224227 }
225228 }
226229 }
227230
228- // step 2: get the constructor with the most parameters
231+ // step 2: get the constructor with the most resolvable parameters
229232 foreach ( ConstructorInfo constructor in constructors )
230233 {
231234 ParameterInfo [ ] parameters = constructor . GetParameters ( ) ;
232235
233236 int length = parameters . Length ;
234237
238+ foreach ( ParameterInfo parameter in parameters )
239+ {
240+ Type type = parameter . ParameterType ;
241+
242+ if ( type . IsResolvable ( ) )
243+ {
244+ // check for simple binding first
245+ }
246+ else if ( GetService ( type ) == null )
247+ {
248+ // binding could not be resolved ingore constructor
249+ length = - 1 ;
250+ }
251+ }
252+
235253 if ( bestLength < length )
236254 {
237255 bestLength = length ;
@@ -243,49 +261,30 @@ private ParameterInfo[] GetParameters(Type implementationType)
243261 }
244262
245263 /// <summary>
246- /// Try and bind to a primitive type.
264+ /// Get primitive default type.
247265 /// </summary>
248- private static bool TryBindToPrimitive ( Type type , out object defaultType )
266+ private static object GetResolvableDefault ( Type type )
249267 {
250- defaultType = null ;
251-
252- // This list dosn't match the binding list below because
268+ // This list dosn't match the IsResolvable() because
253269 // we only check for items we know are not null by default
254- if ( type == typeof ( object ) ) defaultType = default ;
255- if ( type == typeof ( int ) ) defaultType = default ( int ) ;
256- if ( type == typeof ( uint ) ) defaultType = default ( uint ) ;
257- if ( type == typeof ( bool ) ) defaultType = default ( bool ) ;
258- if ( type == typeof ( char ) ) defaultType = default ( char ) ;
259- if ( type == typeof ( byte ) ) defaultType = default ( byte ) ;
260- if ( type == typeof ( sbyte ) ) defaultType = default ( sbyte ) ;
261- if ( type == typeof ( short ) ) defaultType = default ( short ) ;
262- if ( type == typeof ( ushort ) ) defaultType = default ( ushort ) ;
263- if ( type == typeof ( long ) ) defaultType = default ( long ) ;
264- if ( type == typeof ( ulong ) ) defaultType = default ( ulong ) ;
265- if ( type == typeof ( double ) ) defaultType = default ( double ) ;
266- if ( type == typeof ( Guid ) ) defaultType = default ( Guid ) ;
267- if ( type == typeof ( DateTime ) ) defaultType = default ( DateTime ) ;
268- if ( type == typeof ( TimeSpan ) ) defaultType = default ( TimeSpan ) ;
269-
270- return type == typeof ( object )
271- || type == typeof ( string )
272- || type == typeof ( int )
273- || type == typeof ( uint )
274- || type == typeof ( bool )
275- || type == typeof ( char )
276- || type == typeof ( byte )
277- || type == typeof ( sbyte )
278- || type == typeof ( short )
279- || type == typeof ( ushort )
280- || type == typeof ( long )
281- || type == typeof ( ulong )
282- || type == typeof ( double )
283- || type == typeof ( Guid )
284- || type == typeof ( DateTime )
285- || type == typeof ( TimeSpan )
286- || type == typeof ( Enum )
287- || type == typeof ( Array )
288- || type == typeof ( ArrayList ) ;
270+ if ( type == typeof ( object ) ) return default ;
271+ if ( type == typeof ( int ) ) return default ( int ) ;
272+ if ( type == typeof ( uint ) ) return default ( uint ) ;
273+ if ( type == typeof ( bool ) ) return default ( bool ) ;
274+ if ( type == typeof ( char ) ) return default ( char ) ;
275+ if ( type == typeof ( byte ) ) return default ( byte ) ;
276+ if ( type == typeof ( sbyte ) ) return default ( sbyte ) ;
277+ if ( type == typeof ( short ) ) return default ( short ) ;
278+ if ( type == typeof ( ushort ) ) return default ( ushort ) ;
279+ if ( type == typeof ( long ) ) return default ( long ) ;
280+ if ( type == typeof ( ulong ) ) return default ( ulong ) ;
281+ if ( type == typeof ( double ) ) return default ( double ) ;
282+ if ( type == typeof ( float ) ) return default ( float ) ;
283+ if ( type == typeof ( Guid ) ) return default ( Guid ) ;
284+ if ( type == typeof ( DateTime ) ) return default ( DateTime ) ;
285+ if ( type == typeof ( TimeSpan ) ) return default ( TimeSpan ) ;
286+
287+ return null ;
289288 }
290289 }
291290}
0 commit comments