Skip to content

Commit

Permalink
Add support for keyed services. (#3017)
Browse files Browse the repository at this point in the history
* Add support for keyed services in ReflectionUtils.GetMember.

* Remove package reference to DI and add ref to Abstractions in Commands.
  • Loading branch information
AnalogFeelings authored Oct 12, 2024
1 parent de8da0d commit 3017f6b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/Discord.Net.Commands/Discord.Net.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
</ItemGroup>
</Project>
16 changes: 12 additions & 4 deletions src/Discord.Net.Commands/Utilities/ReflectionUtils.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,11 +22,11 @@ internal static Func<IServiceProvider, T> CreateBuilder<T>(TypeInfo typeInfo, Co
{
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<T>(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;
};
}
Expand Down Expand Up @@ -64,13 +65,20 @@ private static PropertyInfo[] GetProperties(TypeInfo ownerType)
}
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<object> attributes)
{
if (memberType == typeof(CommandService))
return commands;
if (memberType == typeof(IServiceProvider) || memberType == services.GetType())
return services;
var service = services.GetService(memberType);

var keyedAttribute = attributes?.FirstOrDefault(x => x.GetType() == typeof(FromKeyedServicesAttribute));
object service;
if (keyedAttribute != null)
service = services.GetKeyedServices(memberType, ((FromKeyedServicesAttribute)keyedAttribute).Key).First();
else
service = services.GetService(memberType);

if (service != null)
return service;
throw new InvalidOperationException($"Failed to create \"{ownerType.FullName}\", dependency \"{memberType.Name}\" was not found.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>
Expand Down
20 changes: 14 additions & 6 deletions src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -23,11 +24,11 @@ internal static Func<IServiceProvider, T> CreateBuilder(TypeInfo typeInfo, Inter
{
var args = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
args[i] = GetMember(commandService, services, parameters[i].ParameterType, typeInfo);
args[i] = GetMember(commandService, services, parameters[i].ParameterType, typeInfo, parameters[i].GetCustomAttributes());

var obj = InvokeConstructor(constructor, args, typeInfo);
foreach (var property in properties)
property.SetValue(obj, GetMember(commandService, services, property.PropertyType, typeInfo));
property.SetValue(obj, GetMember(commandService, services, property.PropertyType, typeInfo, null));
return obj;
};
}
Expand Down Expand Up @@ -66,13 +67,20 @@ private static PropertyInfo[] GetProperties(TypeInfo ownerType)
}
return result.ToArray();
}
private static object GetMember(InteractionService commandService, IServiceProvider services, Type memberType, TypeInfo ownerType)
private static object GetMember(InteractionService commandService, IServiceProvider services, Type memberType, TypeInfo ownerType, IEnumerable<object> attributes)
{
if (memberType == typeof(InteractionService))
return commandService;
if (memberType == typeof(IServiceProvider) || memberType == services.GetType())
return services;
var service = services.GetService(memberType);

var keyedAttribute = attributes?.FirstOrDefault(x => x.GetType() == typeof(FromKeyedServicesAttribute));
object service;
if (keyedAttribute != null)
service = services.GetKeyedServices(memberType, ((FromKeyedServicesAttribute)keyedAttribute).Key).First();
else
service = services.GetService(memberType);

if (service != null)
return service;
throw new InvalidOperationException($"Failed to create \"{ownerType.FullName}\", dependency \"{memberType.Name}\" was not found.");
Expand Down Expand Up @@ -119,10 +127,10 @@ internal static Func<IServiceProvider, T> CreateLambdaBuilder(TypeInfo typeInfo,
var props = new object[properties.Length];

for (int i = 0; i < parameters.Length; i++)
args[i] = GetMember(commandService, services, parameters[i].ParameterType, typeInfo);
args[i] = GetMember(commandService, services, parameters[i].ParameterType, typeInfo, parameters[i].GetCustomAttributes());

for (int i = 0; i < properties.Length; i++)
props[i] = GetMember(commandService, services, properties[i].PropertyType, typeInfo);
props[i] = GetMember(commandService, services, properties[i].PropertyType, typeInfo, null);

var instance = lambda(args, props);

Expand Down

0 comments on commit 3017f6b

Please sign in to comment.