Skip to content

Commit 058f932

Browse files
committed
2 parents 82ba98b + 30b546e commit 058f932

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Library/DependencyInjector.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,26 @@ private void Add<I, T>(ServiceScope scope, string? token = null) where T : I whe
5454
public T[] GetUniques<T>() where T : class => GetServices<T>(ServiceScope.Unique);
5555
public T? GetShared<T>(string? token = null) where T : class => GetServices<T>(ServiceScope.Shared, token).FirstOrDefault();
5656
public T[] GetShares<T>(string? token = null) where T : class => GetServices<T>(ServiceScope.Shared, token);
57-
5857
private T[] GetServices<T>(ServiceScope? scope = null, string? token = null) where T : class
5958
{
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];
6161
}
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)
6363
{
64-
var result = new List<object>();
6564
var serviceKey = new ServiceKey(interfaceType, null, token);
6665
if (!_typesMap.TryGetValue(serviceKey, out var infos))
6766
{
68-
return new object[0];
67+
yield break;
6968
}
7069
foreach (var info in infos)
7170
{
7271
var service = GetService(serviceKey with { Type = info.Type }, info, scope);
7372
if (service is not null)
7473
{
75-
result.Add(service);
74+
yield return service;
7675
}
7776
}
78-
79-
return result.ToArray();
8077
}
8178

8279
private object? GetService(ServiceKey serviceKey, ServiceInfo serviceInfo, ServiceScope? scope = null)
@@ -156,7 +153,11 @@ private ConstructorInfo GetServiceConstructor(ServiceKey serviceKey, ServiceInfo
156153
if (paramType.IsArray)
157154
{
158155
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();
160161
}
161162
return GetServices(paramType, serviceScope, token)?.FirstOrDefault();
162163
}

Tests/DependencyInjectorTest/ExampleClasses.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ internal class MutipleInheranceRef
173173
{
174174
public IMutipleInherance[] Parameters { get; }
175175

176-
public MutipleInheranceRef(IMutipleInherance[] parameters)
176+
public MutipleInheranceRef(IEnumerable<IMutipleInherance> parameters)
177177
{
178-
Parameters = parameters;
178+
Parameters = parameters.ToArray();
179179
}
180180
}

0 commit comments

Comments
 (0)