using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace Comal.TaskScheduler.Shared { public static class SchedulePluginFactory { private static Dictionary cache = new Dictionary(); public static ISchedulePlugin? GetPlugin(String scheduleclass) { if (String.IsNullOrWhiteSpace(scheduleclass)) return null; if (cache.ContainsKey(scheduleclass)) return cache[scheduleclass]; if (!CoreUtils.TryGetEntity(scheduleclass, out var type)) return null; Type defType = typeof(SchedulePlugin<>).MakeGenericType(type); Type? subType = CoreUtils.TypeList( new Assembly[] { typeof(SchedulePluginFactory).Assembly }, myType => myType.IsClass && !myType.IsAbstract && !myType.IsGenericType && myType.IsSubclassOf(defType) ).FirstOrDefault(); if(subType == null) { return null; } ISchedulePlugin result = (ISchedulePlugin)Activator.CreateInstance(subType)!; cache[scheduleclass] = result; return result; } } }