ConsoleDispatcher.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. [CodeProcess]
  6. public class ConsoleDispatcher: Singleton<ConsoleDispatcher>, ISingletonAwake
  7. {
  8. private readonly Dictionary<string, IConsoleHandler> handlers = new();
  9. public void Awake()
  10. {
  11. HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof (ConsoleHandlerAttribute));
  12. foreach (Type type in types)
  13. {
  14. object[] attrs = type.GetCustomAttributes(typeof(ConsoleHandlerAttribute), false);
  15. if (attrs.Length == 0)
  16. {
  17. continue;
  18. }
  19. ConsoleHandlerAttribute consoleHandlerAttribute = (ConsoleHandlerAttribute)attrs[0];
  20. object obj = Activator.CreateInstance(type);
  21. IConsoleHandler iConsoleHandler = obj as IConsoleHandler;
  22. if (iConsoleHandler == null)
  23. {
  24. throw new Exception($"ConsoleHandler handler not inherit IConsoleHandler class: {obj.GetType().FullName}");
  25. }
  26. this.handlers.Add(consoleHandlerAttribute.Mode, iConsoleHandler);
  27. }
  28. }
  29. public IConsoleHandler Get(string key)
  30. {
  31. return this.handlers[key];
  32. }
  33. }
  34. }