From 3017f6b463cfb8b678ae3dbca16fe6f78c8a7408 Mon Sep 17 00:00:00 2001
From: Nora <51166756+AnalogFeelings@users.noreply.github.com>
Date: Sat, 12 Oct 2024 21:44:46 +0200
Subject: [PATCH] Add support for keyed services. (#3017)
* Add support for keyed services in ReflectionUtils.GetMember.
* Remove package reference to DI and add ref to Abstractions in Commands.
---
.../Discord.Net.Commands.csproj | 3 +++
.../Utilities/ReflectionUtils.cs | 16 +++++++++++----
.../Discord.Net.Interactions.csproj | 2 +-
.../Utilities/ReflectionUtils.cs | 20 +++++++++++++------
4 files changed, 30 insertions(+), 11 deletions(-)
diff --git a/src/Discord.Net.Commands/Discord.Net.Commands.csproj b/src/Discord.Net.Commands/Discord.Net.Commands.csproj
index 94e58acf..0f15ea79 100644
--- a/src/Discord.Net.Commands/Discord.Net.Commands.csproj
+++ b/src/Discord.Net.Commands/Discord.Net.Commands.csproj
@@ -19,4 +19,7 @@
+
+
+
diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs
index 28984448..2dcec2d6 100644
--- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs
+++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs
@@ -1,3 +1,4 @@
+using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -21,11 +22,11 @@ namespace Discord.Commands
{
var args = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
- args[i] = GetMember(commands, services, parameters[i].ParameterType, typeInfo);
+ args[i] = GetMember(commands, services, parameters[i].ParameterType, typeInfo, parameters[i].GetCustomAttributes());
var obj = InvokeConstructor(constructor, args, typeInfo);
foreach (var property in properties)
- property.SetValue(obj, GetMember(commands, services, property.PropertyType, typeInfo));
+ property.SetValue(obj, GetMember(commands, services, property.PropertyType, typeInfo, null));
return obj;
};
}
@@ -64,13 +65,20 @@ namespace Discord.Commands
}
return result.ToArray();
}
- private static object GetMember(CommandService commands, IServiceProvider services, Type memberType, TypeInfo ownerType)
+ private static object GetMember(CommandService commands, IServiceProvider services, Type memberType, TypeInfo ownerType, IEnumerable