Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using CommandPattern.Core.Contracts;
- using System;
- using System.Linq;
- using System.Reflection;
- namespace CommandPattern.Core
- {
- public class Engine : IEngine
- {
- private readonly ICommandInterpreter commandInterpreter;
- public Engine(ICommandInterpreter command)
- {
- this.commandInterpreter = command;
- }
- public void Run()
- {
- while (true)
- {
- var input = Console.ReadLine();
- var result = this.commandInterpreter.Read(input);
- Console.WriteLine(result);
- }
- }
- }
- public class CommandInterpreter : ICommandInterpreter
- {
- public string Read(string args)
- {
- var data = args.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- var com = (data[0]+ "Command").ToLower();
- var comArgs = data.Skip(1).ToArray();
- var type = Assembly.GetCallingAssembly().GetTypes().FirstOrDefault(x => x.Name.ToLower() == com);
- if (type == null) throw new ArgumentException("Invalid command type!");
- var instance = Activator.CreateInstance(type) as ICommand;
- if (instance == null) throw new ArgumentException("Invalid command type!");
- var result = instance.Execute(comArgs);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment