Beispiel #1
0
        public string ListAllCommands()
        {
            List <string> sb = new List <string>();

            foreach (Assembly assembly in assemblies)
            {
                foreach (Type t in assembly.GetTypes())
                {
                    if (t.IsClass && typeof(Computer.Computer).IsAssignableFrom(t))
                    {
                        try
                        {
                            Computer.Command commandObj = (Computer.Command)Activator.CreateInstance(t);
                            sb.Add(commandObj.Name + "\n");
                        }
                        //Do nothing if Name is not implemented
                        // if Name is not implemented, it means it is not a command we can use
                        catch (NotImplementedException) {}
                    }
                }
            }

            //Orders the list of string in an alphabetical order, then joins every string into one big string
            // then Trims the end of it, to contains no new line
            return(String.Join("", sb.OrderBy(str => str)).TrimEnd(Environment.NewLine.ToCharArray()));
        }
Beispiel #2
0
        //Instatiates a class and calls it's Execute() method
        private string ExecuteMethod(string cmdName, string[] args)
        {
            string result = "";

            try
            {
                Type             commandClass = ReturnCommand(cmdName);
                Computer.Command commandObj   = (Computer.Command)Activator.CreateInstance(commandClass);
                if (args.Count() > 0 && args[0] == "-h")
                {
                    result = commandObj.Help;
                }
                else
                {
                    result = commandObj.Execute(args).ToString();
                }
            }
            catch (Exception e)
            {
                result = e.Message;
            }

            return(result);
        }