You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DI.cs 722 B

12345678910111213141516171819202122232425262728
  1. public class MyService
  2. {
  3. public string MyCoolString { get; set; }
  4. }
  5. public class Setup
  6. {
  7. public IServiceProvider BuildProvider() =>
  8. new ServiceCollection()
  9. .AddSingleton<MyService>()
  10. .BuildServiceProvider();
  11. }
  12. public class MyModule : ModuleBase<SocketCommandContext>
  13. {
  14. // Inject via public settable prop
  15. public MyService MyService { get; set; }
  16. // ...or via the module's constructor
  17. // private readonly MyService _myService;
  18. // public MyModule (MyService myService) => _myService = myService;
  19. [Command("string")]
  20. public Task GetOrSetStringAsync(string input)
  21. {
  22. if (string.IsNullOrEmpty(_myService.MyCoolString)) _myService.MyCoolString = input;
  23. return ReplyAsync(_myService.MyCoolString);
  24. }
  25. }