Advertisement
StoyanGrigorov

Reflection c#

Mar 30th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TeamBuilder.Client.Core.Commands;
  8. using TeamBuilder.Services;
  9.  
  10. namespace TeamBuilder.Client.Core
  11. {
  12.     public class CommandDispatcher
  13.     {
  14.         public string DispatchCommand(string[] commandParameters)
  15.         {
  16.             string commandName = commandParameters[0];
  17.             commandParameters = commandParameters.Skip(1).ToArray();
  18.  
  19.             string result = string.Empty;
  20.  
  21.             Type commandType = Type.GetType("TeamBuilder.Client.Core.Commands." + commandName + "Command");
  22.  
  23.             if (commandType == null)
  24.             {
  25.                 throw new NotSupportedException($"Command {commandName} not supported!");
  26.             }
  27.  
  28.             object command = Activator.CreateInstance(commandType);
  29.  
  30.             MethodInfo executeMethod = command.GetType().GetMethod("Execute");
  31.  
  32.             result = executeMethod.Invoke(command, new object[] { commandParameters }) as string;
  33.  
  34.             switch (commandName)
  35.             {
  36.                 case "RegisterUser":
  37.                     RegisterUserCommand registerUser = new RegisterUserCommand(new UserService());
  38.                     result = registerUser.Execute(commandParameters);
  39.                     break;
  40.                 case "Login":
  41.                     LoginCommand loginCommand = new LoginCommand(new UserService());
  42.                     result = loginCommand.Execute(commandParameters);
  43.                     break;
  44.                 case "Logout":
  45.                     LogoutCommand logoutCommand = new LogoutCommand(new UserService());
  46.                     result = logoutCommand.Execute(commandParameters);
  47.                     break;
  48.                 case "DeleteUser":
  49.                     DeleteUserCommand deleteUser = new DeleteUserCommand(new UserService());
  50.                     result = deleteUser.Execute(commandParameters);
  51.                     break;
  52.                 case "CreateEvent":
  53.                     CreateEventCommand createEvent = new CreateEventCommand(new EventService());
  54.                     result = createEvent.Execute(commandParameters);
  55.                     break;
  56.                 case "CreateTeam":
  57.                     CreateTeamCommand createTeam = new CreateTeamCommand(new TeamService());
  58.                     result = createTeam.Execute(commandParameters);
  59.                     break;
  60.                 case "InviteToTeam":
  61.                     InviteToTeamCommand invToTeam = new InviteToTeamCommand(new TeamService(), new UserService());
  62.                     result = invToTeam.Execute(commandParameters);
  63.                     break;
  64.                 case "AcceptInvite":
  65.                     AcceptInviteCommand acceptInvite = new AcceptInviteCommand(new TeamService(), new UserService());
  66.                     result = acceptInvite.Execute(commandParameters);
  67.                     break;
  68.                 case "DeclineInvite":
  69.                     DeclineInviteCommand declineInvite = new DeclineInviteCommand(new TeamService(), new UserService());
  70.                     result = declineInvite.Execute(commandParameters);
  71.                     break;
  72.                 case "KickMember":
  73.                     KickMemberCommand kickMember = new KickMemberCommand(new TeamService(), new UserService());
  74.                     result = kickMember.Execute(commandParameters);
  75.                     break;
  76.                 case "Disband":
  77.                     DisbandCommand disbandTeam = new DisbandCommand(new TeamService(), new UserService());
  78.                     result = disbandTeam.Execute(commandParameters);
  79.                     break;
  80.                 case "AddTeamTo":
  81.                     AddTeamToCommand addTeam = new AddTeamToCommand(new EventService(), new TeamService());
  82.                     result = addTeam.Execute(commandParameters);
  83.                     break;
  84.                 case "ShowEvent":
  85.                     ShowEventCommand showEvent = new ShowEventCommand(new EventService());
  86.                     result = showEvent.Execute(commandParameters);
  87.                     break;
  88.                 case "ShowTeam":
  89.                 case "Exit":
  90.                     ExitCommand exit = new ExitCommand();
  91.                     exit.Execute(commandParameters);
  92.                     break;
  93.                 default:
  94.                     throw new NotSupportedException($"Command {commandName} not valid!");
  95.             }
  96.  
  97.             return result;
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement