using GameHandler.Exceptions; using GameModel.Contract; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace GameHandler.Extensions { public static class GameServiceExtension { public static IGameHandler GetGameHandler(this GameService gameService, string gameName) { var handlers = gameService.GetGameHandler(); if (!handlers.ContainsKey(gameName)) { throw new InvaildGameNameException($"not handler found for {gameName}"); } var type = handlers[gameName]; var res = (IGameHandler)Activator.CreateInstance(type); return res; } public static Dictionary GetGameHandler(this GameService gameService) { var res = new Dictionary(); var type = typeof(IGameHandler); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => type.IsAssignableFrom(p) && (!p.IsInterface)); foreach (var item in types) { MethodInfo staticMethodInfo = item.GetMethod("GameName"); string returnValue = Convert.ToString(staticMethodInfo.Invoke(null, Array.Empty())); res.Add(returnValue, item); } return res; } } }