Skip to content

Instantly share code, notes, and snippets.

@hugodahl
Created May 19, 2022 03:23
Show Gist options
  • Save hugodahl/7471b761a804dd6120f371d7d75e316f to your computer and use it in GitHub Desktop.
Save hugodahl/7471b761a804dd6120f371d7d75e316f to your computer and use it in GitHub Desktop.
Getting a list of registered services from DI
// This is the thing that does the work, or the "!command" command
public class GetAllCommandsCommand : ICommandMbhToTwitch
{
public GetAllCommandsCommand(IOptionsSnapshot<List<string>> commandNames)
{
var commands = commandNames.Get("CommandNames");
this.CommandNames = commands;
}
public IEnumerable<string> CommandNames { get; set; }
public string Name => "command";
public void Handle(OnChatCommandReceivedArgs args)
{
Console.WriteLine(this.CommandNames);
}
}
public static IHostBuilder CreateHostBuilder(string[] args) => {
// Some clever bits happen here....
services.AddOptions<List<string>>("CommandNames").Configure<IServiceProvider>((str, svc) =>
{
var commandNames = svc.GetServices<ICommandMbhToTwitch>().Select(cmd => cmd.Name);
str.AddRange(commandNames);
});
// Do some more clever stuff with services
// Register the command that gets all commands
services.AddScoped<GetAllCommandsCommand>();
// Finish setup, registration, etc.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment