SchedulePluginFactory.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace Comal.TaskScheduler.Shared
  8. {
  9. public static class SchedulePluginFactory
  10. {
  11. private static Dictionary<String, ISchedulePlugin> cache = new Dictionary<string, ISchedulePlugin>();
  12. public static ISchedulePlugin? GetPlugin(String scheduleclass)
  13. {
  14. if (String.IsNullOrWhiteSpace(scheduleclass))
  15. return null;
  16. if (cache.ContainsKey(scheduleclass))
  17. return cache[scheduleclass];
  18. if (!CoreUtils.TryGetEntity(scheduleclass, out var type))
  19. return null;
  20. Type defType = typeof(SchedulePlugin<>).MakeGenericType(type);
  21. Type? subType = CoreUtils.TypeList(
  22. new Assembly[] { typeof(SchedulePluginFactory).Assembly },
  23. myType =>
  24. myType.IsClass
  25. && !myType.IsAbstract
  26. && !myType.IsGenericType
  27. && myType.IsSubclassOf(defType)
  28. ).FirstOrDefault();
  29. if(subType == null)
  30. {
  31. return null;
  32. }
  33. ISchedulePlugin result = (ISchedulePlugin)Activator.CreateInstance(subType)!;
  34. cache[scheduleclass] = result;
  35. return result;
  36. }
  37. }
  38. }