예제 #1
0
 /// <summary>
 ///   Draws a method summary to the DebugConsole.
 /// </summary>
 /// <param name="method">
 ///   The method to be explained.
 /// </param>
 /// <param name="params">
 ///   The parameters of the method.
 /// </param>
 private static void DrawHelp(string method, ParameterInfo[] @params)
 {
     DebugCon.WriteLine("Usage for " + method);
     foreach (var p in @params)
     {
         DebugCon.WriteLine(p.Name + " typeof " + p.ParameterType.ToString());
     }
 }
예제 #2
0
        /// <summary>
        ///   Handle the line from the console.
        ///   Executes the method or prints an error to the DebugConsole.
        /// </summary>
        /// <param name="line">
        ///   The line to be handled.
        /// </param>
        public static void OnInput(string line)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return;
            }

            // Get the args from the line
            // Example: Set property value
            var        args = SplitArguments(line);
            MethodInfo meth;

            try
            {
                meth = GetMethod(args[0].ToString());
            }
            catch (FieldAccessException ex)
            {
                // Method not found
                DebugCon.WriteError(ex.Message);
                return;
            }

            // Init a new array with the method arguments.
            // Example: property value
            var args2 = new object[args.Length - 1];

            Array.Copy(args, 1, args2, 0, args.Length - 1);

            // Get the parameters of the method
            var param = meth.GetParameters();

            // Error if parameter length does not match
            if (param.Length != args2.Length)
            {
                DrawHelp(meth.Name, param);
                return;
            }

            // Convert every parameter into the correct type
            for (var i = 0; i < param.Length; i++)
            {
                args2[i] = Convert.ChangeType(args2[i], param[i].ParameterType, System.Globalization.CultureInfo.InvariantCulture);
            }

            try
            {
                // Try to invoke the method and print the return value
                var val = meth.Invoke(null, args2);
                if (val == null)
                {
                    return;
                }

                DebugCon.WriteLine(args[0] + " returned " + val.ToString());
            }
            catch (Exception ex)
            {
                // Something went wrong
                DebugCon.WriteError((ex.InnerException ?? ex).Message);
            }
        }