using System; using System.Collections.Generic; using System.Linq; using VAR.ScreenAutomation.Bots; using VAR.ScreenAutomation.Interfaces; namespace VAR.ScreenAutomation.Code { public static class AutomationBotFactory { private static Dictionary _dictAutomationBots; private static Dictionary GetDict() { if (_dictAutomationBots != null) { return _dictAutomationBots; } Type iAutomationBot = typeof(IAutomationBot); IEnumerable automationBotTypes = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(x => x.GetTypes()) .Where(x => x.IsAbstract == false && x.IsInterface == false && iAutomationBot.IsAssignableFrom(x) && true); _dictAutomationBots = automationBotTypes.ToDictionary(t => { IAutomationBot automationBot = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IAutomationBot; return automationBot?.Name ?? t.Name; }); return _dictAutomationBots; } public static object[] GetAllAutomationBots() { Dictionary dict = GetDict(); string[] allAutomationBots = dict.Select(p => p.Key).ToArray(); return allAutomationBots.ToArray(); } public static IAutomationBot CreateFromName(string name) { Dictionary dict = GetDict(); if (string.IsNullOrEmpty(name)) { return new DummyBot(); } if (dict.ContainsKey(name) == false) { throw new NotImplementedException($"Can't create IAutomationBot with this name: {name}"); } Type proxyCmdExecutorType = dict[name]; IAutomationBot automationBot = Activator.CreateInstance(proxyCmdExecutorType) as IAutomationBot; return automationBot; } } }