Skip to content

Commit d2d5c7d

Browse files
committed
Add keyed service support
1 parent 87b2efe commit d2d5c7d

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/HeyBox.Net.Interactions/Utilities/ReflectionUtils.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq.Expressions;
22
using System.Reflection;
3+
using Microsoft.Extensions.DependencyInjection;
34

45
namespace HeyBox.Interactions;
56

@@ -21,13 +22,13 @@ internal static T CreateObject(TypeInfo typeInfo, InteractionService commandServ
2122

2223
return (services) =>
2324
{
24-
object[] args = new object[parameters.Length];
25-
for (int i = 0; i < parameters.Length; i++)
26-
args[i] = GetMember(commandService, services, parameters[i].ParameterType, typeInfo);
25+
object[] args = parameters
26+
.Select(x => GetMember(commandService, services, x.ParameterType, typeInfo, x.GetCustomAttributes()))
27+
.ToArray();
2728

2829
T obj = InvokeConstructor(constructor, args, typeInfo);
2930
foreach (PropertyInfo property in properties)
30-
property.SetValue(obj, GetMember(commandService, services, property.PropertyType, typeInfo));
31+
property.SetValue(obj, GetMember(commandService, services, property.PropertyType, typeInfo, null));
3132
return obj;
3233
};
3334
}
@@ -67,15 +68,14 @@ private static PropertyInfo[] GetProperties(TypeInfo ownerType)
6768
}
6869
return result.ToArray();
6970
}
70-
private static object GetMember(InteractionService commandService, IServiceProvider? services, Type memberType, TypeInfo ownerType)
71+
private static object GetMember(InteractionService commandService, IServiceProvider? services, Type memberType, TypeInfo ownerType, IEnumerable<object>? attributes)
7172
{
72-
if (memberType == typeof(InteractionService))
73-
return commandService;
74-
if (services is not null && (memberType == typeof(IServiceProvider) || memberType == services.GetType()))
75-
return services;
76-
object? service = services?.GetService(memberType);
77-
if (service != null)
78-
return service;
73+
if (memberType == typeof(InteractionService)) return commandService;
74+
if (services is not null && (memberType == typeof(IServiceProvider) || memberType == services.GetType())) return services;
75+
object? service = attributes?.FirstOrDefault(x => x.GetType() == typeof(FromKeyedServicesAttribute)) is { } keyedAttribute
76+
? services?.GetKeyedServices(memberType, ((FromKeyedServicesAttribute)keyedAttribute).Key).First()
77+
: services?.GetService(memberType);
78+
if (service != null) return service;
7979
throw new InvalidOperationException($"Failed to create \"{ownerType.FullName}\", dependency \"{memberType.Name}\" was not found.");
8080
}
8181

@@ -117,17 +117,13 @@ private static object GetMember(InteractionService commandService, IServiceProvi
117117

118118
return (services) =>
119119
{
120-
object[] args = new object[parameters.Length];
121-
object[] props = new object[properties.Length];
122-
123-
for (int i = 0; i < parameters.Length; i++)
124-
args[i] = GetMember(commandService, services, parameters[i].ParameterType, typeInfo);
125-
126-
for (int i = 0; i < properties.Length; i++)
127-
props[i] = GetMember(commandService, services, properties[i].PropertyType, typeInfo);
128-
120+
object[] args = parameters
121+
.Select(x => GetMember(commandService, services, x.ParameterType, typeInfo, x.GetCustomAttributes()))
122+
.ToArray();
123+
object[] props = properties
124+
.Select(x => GetMember(commandService, services, x.PropertyType, typeInfo, null))
125+
.ToArray();
129126
T instance = lambda(args, props);
130-
131127
return instance;
132128
};
133129
}

0 commit comments

Comments
 (0)