@@ -54,29 +54,26 @@ private void Add<I, T>(ServiceScope scope, string? token = null) where T : I whe
54
54
public T [ ] GetUniques < T > ( ) where T : class => GetServices < T > ( ServiceScope . Unique ) ;
55
55
public T ? GetShared < T > ( string ? token = null ) where T : class => GetServices < T > ( ServiceScope . Shared , token ) . FirstOrDefault ( ) ;
56
56
public T [ ] GetShares < T > ( string ? token = null ) where T : class => GetServices < T > ( ServiceScope . Shared , token ) ;
57
-
58
57
private T [ ] GetServices < T > ( ServiceScope ? scope = null , string ? token = null ) where T : class
59
58
{
60
- return GetServices ( typeof ( T ) , ServiceScope . Shared , token ) as T [ ] ?? new T [ 0 ] ;
59
+ var services = GetServices ( typeof ( T ) , scope , token ) ;
60
+ return Array . ConvertAll ( services . ToArray ( ) , item => item as T ) ?? new T [ 0 ] ;
61
61
}
62
- private object [ ] GetServices ( Type interfaceType , ServiceScope ? scope = null , string ? token = null )
62
+ private IEnumerable < object > GetServices ( Type interfaceType , ServiceScope ? scope = null , string ? token = null )
63
63
{
64
- var result = new List < object > ( ) ;
65
64
var serviceKey = new ServiceKey ( interfaceType , null , token ) ;
66
65
if ( ! _typesMap . TryGetValue ( serviceKey , out var infos ) )
67
66
{
68
- return new object [ 0 ] ;
67
+ yield break ;
69
68
}
70
69
foreach ( var info in infos )
71
70
{
72
71
var service = GetService ( serviceKey with { Type = info . Type } , info , scope ) ;
73
72
if ( service is not null )
74
73
{
75
- result . Add ( service ) ;
74
+ yield return service ;
76
75
}
77
76
}
78
-
79
- return result . ToArray ( ) ;
80
77
}
81
78
82
79
private object ? GetService ( ServiceKey serviceKey , ServiceInfo serviceInfo , ServiceScope ? scope = null )
@@ -156,7 +153,11 @@ private ConstructorInfo GetServiceConstructor(ServiceKey serviceKey, ServiceInfo
156
153
if ( paramType . IsArray )
157
154
{
158
155
paramType = paramType . GetElementType ( ) ;
159
- return GetServices ( paramType ?? typeof ( object ) , serviceScope , token ) ;
156
+ var listType = typeof ( List < > ) . MakeGenericType ( paramType ) ;
157
+ dynamic result = Activator . CreateInstance ( listType ) ;
158
+ var cast = result as List < object > ;
159
+ cast . AddRange ( GetServices ( paramType ?? typeof ( object ) , serviceScope , token ) ) ;
160
+ return result . ToArray ( ) ;
160
161
}
161
162
return GetServices ( paramType , serviceScope , token ) ? . FirstOrDefault ( ) ;
162
163
}
0 commit comments