@@ -21,12 +21,12 @@ private enum ServiceScope
21
21
Shared ,
22
22
Unique ,
23
23
}
24
- private record ServiceInfo ( Type ? Type , ServiceScope Scope )
24
+ private record ServiceInfo ( Type Type , ServiceScope Scope )
25
25
{
26
26
public bool IsShared => Scope == ServiceScope . Shared ;
27
27
public bool IsUnique => Scope == ServiceScope . Unique ;
28
28
}
29
- private record ServiceKey ( Type ? IType , Type ? Type , string ? Token )
29
+ private record ServiceKey ( Type IType , Type ? Type , string ? Token )
30
30
{
31
31
public override int GetHashCode ( ) => IType . GetHashCode ( ) + Type ? . GetHashCode ( ) ?? 0 + Token ? . GetHashCode ( ) ?? 0 ;
32
32
}
@@ -51,28 +51,32 @@ private void Add<I, T>(ServiceScope scope, string? token = null) where T : I whe
51
51
}
52
52
}
53
53
public T ? GetUnique < T > ( ) where T : class => GetServices < T > ( ServiceScope . Unique ) . FirstOrDefault ( ) ;
54
- public List < T > GetUniques < T > ( ) where T : class => GetServices < T > ( ServiceScope . Unique ) ;
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
- public List < T > GetShares < T > ( string ? token = null ) where T : class => GetServices < T > ( ServiceScope . Shared , token ) ;
57
- private List < T > GetServices < T > ( ServiceScope ? scope = null , string ? token = null , Type ? interfaceType = null ) where T : class
56
+ public T [ ] GetShares < T > ( string ? token = null ) where T : class => GetServices < T > ( ServiceScope . Shared , token ) ;
57
+
58
+ private T [ ] GetServices < T > ( ServiceScope ? scope = null , string ? token = null ) where T : class
59
+ {
60
+ return GetServices ( typeof ( T ) , ServiceScope . Shared , token ) as T [ ] ?? new T [ 0 ] ;
61
+ }
62
+ private object [ ] GetServices ( Type interfaceType , ServiceScope ? scope = null , string ? token = null )
58
63
{
59
- interfaceType ??= typeof ( T ) ;
60
- var result = new List < T > ( ) ;
64
+ var result = new List < object > ( ) ;
61
65
var serviceKey = new ServiceKey ( interfaceType , null , token ) ;
62
66
if ( ! _typesMap . TryGetValue ( serviceKey , out var infos ) )
63
67
{
64
- return new List < T > ( ) ;
68
+ return new object [ 0 ] ;
65
69
}
66
70
foreach ( var info in infos )
67
71
{
68
- var service = GetService ( serviceKey with { Type = info . Type } , info , scope ) as T ;
72
+ var service = GetService ( serviceKey with { Type = info . Type } , info , scope ) ;
69
73
if ( service is not null )
70
74
{
71
75
result . Add ( service ) ;
72
76
}
73
77
}
74
78
75
- return result ;
79
+ return result . ToArray ( ) ;
76
80
}
77
81
78
82
private object ? GetService ( ServiceKey serviceKey , ServiceInfo serviceInfo , ServiceScope ? scope = null )
@@ -152,9 +156,8 @@ private ConstructorInfo GetServiceConstructor(ServiceKey serviceKey, ServiceInfo
152
156
if ( paramType . IsArray )
153
157
{
154
158
paramType = paramType . GetElementType ( ) ;
155
- return GetServices < object > ( serviceScope , token , paramType ) . ToArray ( ) ;
156
-
159
+ return GetServices ( paramType ?? typeof ( object ) , serviceScope , token ) ;
157
160
}
158
- return GetServices < object > ( serviceScope , token , paramType ) ? . FirstOrDefault ( ) ;
161
+ return GetServices ( paramType , serviceScope , token ) ? . FirstOrDefault ( ) ;
159
162
}
160
163
}
0 commit comments