@@ -284,8 +284,25 @@ private object ResolveTypeToObject(Type implementType, DependencyResolveOption?
284284
285285
286286 var parameterInfos = constructorInfo . GetParameters ( ) ;
287- var parameters = new List < object > ( ) ;
287+ var parameters = ResolveParameterInfos ( implementType , parameterInfos , option ) ;
288+
289+ var dependencyImpl = constructorInfo . Invoke ( parameters . ToArray ( ) ) ;
290+ if ( option ? . FatherImplementations is { Count : > 0 } )
291+ {
292+ foreach ( var kvp in option . FatherImplementations )
293+ {
294+ _fatherToChildRelation . GetOrCreateValue ( kvp . Value ) . Add ( new WeakReference ( dependencyImpl ) ) ;
295+ _childToFatherRelation . GetOrCreateValue ( dependencyImpl ) . Add ( new WeakReference ( kvp . Value ) ) ;
296+ }
297+ }
298+
299+ return dependencyImpl ;
300+ }
301+
302+ public List < object > ResolveParameterInfos ( Type implementType , ParameterInfo [ ] parameterInfos , DependencyResolveOption ? option )
303+ {
288304 var previousThrowWhenNotExists = true ;
305+ var parameters = new List < object > ( ) ;
289306 foreach ( var parameterInfo in parameterInfos )
290307 {
291308 if ( option ? . FatherImplementations ? . TryGetValue ( parameterInfo . ParameterType , out var impl ) is true )
@@ -305,7 +322,7 @@ private object ResolveTypeToObject(Type implementType, DependencyResolveOption?
305322 if ( msattr is not null )
306323 {
307324 var key = msattr . GetType ( ) . GetProperty ( "Key" ) ? . GetValue ( msattr ) ;
308- option . RelationName = $ " { key ? . GetType ( ) } : { key ? . GetHashCode ( ) } " ;
325+ option . RelationName = SafeToString ( key ) ;
309326 }
310327 }
311328 if ( parameterInfo . GetCustomAttributes ( ) . FirstOrDefault ( t => t is FromNamedServiceAttribute ) is
@@ -337,20 +354,9 @@ private object ResolveTypeToObject(Type implementType, DependencyResolveOption?
337354 $ "The constructor of { implementType . Name } contains a parameter called { parameterInfo . Name } ({ parameterInfo . Position } ) which cannot resolved") ;
338355 }
339356 }
340-
341357 if ( option is not null )
342358 option . ThrowWhenNotExists = previousThrowWhenNotExists ;
343- var dependencyImpl = constructorInfo . Invoke ( parameters . ToArray ( ) ) ;
344- if ( option ? . FatherImplementations is { Count : > 0 } )
345- {
346- foreach ( var kvp in option . FatherImplementations )
347- {
348- _fatherToChildRelation . GetOrCreateValue ( kvp . Value ) . Add ( new WeakReference ( dependencyImpl ) ) ;
349- _childToFatherRelation . GetOrCreateValue ( dependencyImpl ) . Add ( new WeakReference ( kvp . Value ) ) ;
350- }
351- }
352-
353- return dependencyImpl ;
359+ return parameters ;
354360 }
355361
356362
@@ -418,4 +424,23 @@ private object ResolveSingleton(Type implementType, DependencyResolveOption? opt
418424 RootScope . SetImplementation ( implementType , impl , option ? . RelationName ) ;
419425 return impl ;
420426 }
427+
428+ internal static string SafeToString ( object ? obj )
429+ {
430+ if ( obj == null )
431+ return "null" ;
432+
433+ var type = obj . GetType ( ) ;
434+ var toStringMethod = type . GetMethod ( "ToString" , Type . EmptyTypes ) ;
435+
436+ // 检查是否在当前类型中重写了 ToString(排除继承自 object 的)
437+ if ( toStringMethod != null && toStringMethod . DeclaringType != typeof ( object ) )
438+ {
439+ return obj . ToString ( ) ;
440+ }
441+ else
442+ {
443+ return $ "{ type . FullName } @{ obj . GetHashCode ( ) : X} ";
444+ }
445+ }
421446}
0 commit comments