46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
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<string, Type> GetGameHandler(this GameService gameService)
|
|
{
|
|
var res = new Dictionary<string, Type>();
|
|
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<object>()));
|
|
|
|
res.Add(returnValue, item);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
}
|
|
}
|