/// <summary> /// Runs the specified example name. /// </summary> /// <param name="exampleName">Name of the example.</param> /// <param name="args">The arguments.</param> /// <exception cref="KeyNotFoundException">Thrown if the example with the specified name /// is not found.</exception> public void Run(string exampleName, IEnumerable <string> args) { SystemType codeExampleType = GetCodeExampleType(exampleName); if (codeExampleType != null) { Console.WriteLine($"Requested: '{exampleName}', Loaded: '" + $"{ExampleBase.GetVersionedName(codeExampleType)}'."); } else { throw new KeyNotFoundException($"Code example not found: '{exampleName}'."); } MethodBase method = codeExampleType.GetMethod("Main", BindingFlags.Static | BindingFlags.Public); if (method == null) { throw new MissingMethodException($"Main method not found in example: " + $"'{exampleName}'."); } try { method.Invoke(null, new object[] { args.ToArray() }); } catch (Exception e) { StackTrace stackTrace = new StackTrace(e.InnerException); StackFrame frame = stackTrace.GetFrame(0); if (frame.GetMethod() == method) { // The site of the exception was the main method itself. So there was an error // calling the Run() method, typically due to argument errors. throw new ArgumentException("Could not call the Run() method from Main(). " + "Check your arguments."); } throw e.InnerException; } }