/// <summary> /// Executes the CMD. /// </summary> /// <param name="mi">The mi.</param> /// <param name="Parameters">The parameters.</param> /// <returns></returns> private static string ExecuteCmd(CmdMethodInfo mi, object instance, object[] parameters) { object obj2 = mi.methodInfo.Invoke(instance != null ? instance : mi.instance, parameters); return ((obj2 != null) ? obj2.ToString() : string.Empty); }
/// <summary> /// Executes the specified command. /// </summary> /// <param name="Command">The command.</param> /// <returns></returns> private static string Execute(string command, object instance, bool excute, out CmdMethodInfo cmdInfo) { cmdInfo = null; string str = string.Empty; object[] cmdParams = SplitParams(command); if (cmdParams.Length == 0) { return string.Empty; } for (int i = 0; i < cmdParams.Length; i++) { if (cmdParams[i] is ConsoleCommandWrapper) { CmdMethodInfo tmp; cmdParams[i] = Execute(((ConsoleCommandWrapper)cmdParams[i]).command, instance, true, out tmp); } } if (!(cmdParams[0] is string)) { throw new ConsoleCommandException("Unknown command \"" + cmdParams[0].ToString() + "\""); } string key = ((string)cmdParams[0]).ToLower(); /*if ((key == "help") || (key == "?")) { if (cmdParams.Length == 1) return this.buildHelpString(); if (cmdParams.Length == 2) return this.buildCommandHelpString((string)cmdParams[1]); }*/ IList list = null; List<CmdMethodInfo> list2 = null; if (consoleMethods.TryGetValue(key, out list2)) { list = consoleMethods[key]; } else { char[] separator = new char[] { '.' }; string[] strArray = key.Split(separator); if (strArray.Length == 2) { list = FindStaticMethods(strArray[0], strArray[1]); } } if ((list == null) || (list.Count <= 0)) { throw new ConsoleCommandException("Unknown command \"" + cmdParams[0] + "\""); } object[] parameters = null; foreach(CmdMethodInfo info in list) { if (ParametersMatch(info.methodInfo.GetParameters(), cmdParams, out parameters)) { cmdInfo = info; if( excute ) return ExecuteCmd(info, instance, parameters); return ""; } } if ((cmdParams.Length > 1) && (cmdParams[1] is object[])) { object[] destinationArray = new object[((object[])cmdParams[1]).Length + 1]; destinationArray[0] = cmdParams[0]; Array.Copy((object[])cmdParams[1], 0, destinationArray, 1, ((object[])cmdParams[1]).Length); foreach(CmdMethodInfo info in list) { if (ParametersMatch(info.methodInfo.GetParameters(), destinationArray, out parameters)) { cmdInfo = info; if (excute) return ExecuteCmd(info, instance, parameters); return ""; } } } str = "Could not find matching parameter list for command \"" + cmdParams[0] + "\" with parameters ["; for (int j = 1; j < cmdParams.Length; j++) { if (j > 1) { str = str + " "; } str = str + cmdParams[j].ToString(); } throw new ConsoleCommandException(str + "]."); }