SchedulePluginFactory.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. Type type;
  19. try
  20. {
  21. type = CoreUtils.GetEntity(scheduleclass);
  22. }
  23. catch
  24. {
  25. return null;
  26. }
  27. Type defType = typeof(SchedulePlugin<>).MakeGenericType(type);
  28. Type? subType = CoreUtils.TypeList(
  29. new Assembly[] { typeof(SchedulePluginFactory).Assembly },
  30. myType =>
  31. myType.IsClass
  32. && !myType.IsAbstract
  33. && !myType.IsGenericType
  34. && myType.IsSubclassOf(defType)
  35. ).FirstOrDefault();
  36. if(subType == null)
  37. {
  38. return null;
  39. }
  40. ISchedulePlugin result = (ISchedulePlugin)Activator.CreateInstance(subType)!;
  41. cache[scheduleclass] = result;
  42. return result;
  43. }
  44. }
  45. }