| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System.Reflection;
- using Microsoft.Extensions.DependencyInjection;
- using Spectre.Console.Cli;
- namespace Shared.Rcl;
- public static class ServiceCollectionExtensions
- {
- public static IServiceCollection AddCommands(
- this IServiceCollection services)
- {
- var commandTypes = Assembly.GetAssembly(typeof(ServiceCollectionExtensions))
- ?.GetTypes()
- .Where(t =>
- t is { IsAbstract: false, IsInterface: false } &&
- IsAsyncCommand(t)
- );
- foreach (var type in commandTypes) services.AddScoped(type);
- return services;
- }
- private static bool IsAsyncCommand(Type type)
- {
- while (type != null)
- {
- if (type.IsGenericType &&
- type.GetGenericTypeDefinition() == typeof(AsyncCommand<>))
- return true;
- if (type == typeof(AsyncCommand))
- return true;
- type = type.BaseType!;
- }
- return false;
- }
- }
|