/// <summary> /// Finds the code examples in a given assembly. /// </summary> /// <param name="assembly">The assembly to search for.</param> /// <param name="typesToExclude">List of types to exclude.</param> public void LoadCodeExamples(Assembly assembly, SystemType[] typesToExclude) { lock (codeExampleMap) { codeExampleMap.Clear(); SystemType[] types = assembly.GetTypes(); foreach (SystemType type in types) { if (type.IsSubclassOf(typeof(ExampleBase)) && !typesToExclude.Contains(type)) { ExampleBase example = (ExampleBase)Activator.CreateInstance(type); codeExampleMap.Add(example.Name, example); } } } }
/// <summary> /// Finds the code examples. /// </summary> /// <param name="assembly">The assembly.</param> public void LoadCodeExamples(Assembly assembly) { SystemType[] types = assembly.GetTypes(); ExampleBase codeExample = null; foreach (SystemType type in types) { if (type.IsSubclassOf(typeof(ExampleBase))) { ExampleBase example = (ExampleBase)Activator.CreateInstance(type); if (!codeExampleMap.TryGetValue(example.Name, out codeExample)) { codeExampleMap.Add(example.Name, example); } } } }
/// <summary> /// Gets the flags from <code>Run</code> method signature of a code example. /// </summary> /// <param name="codeExample">The code example.</param> /// <returns>A list of flags for running this code example.</returns> private static Flag[] GetFlagsFromRunMethodSignature(ExampleBase codeExample) { // Get the Run method. MethodInfo runMethod = GetRunMethod(codeExample); if (runMethod == null) { throw new MissingMethodException( $"Run method is missing in the example: {codeExample.Name}."); } List<Flag> retval = new List<Flag>(); // Skip one parameter, since the first parameter is the GoogleAdsClient. foreach(ParameterInfo parameterInfo in runMethod.GetParameters().Skip(1)) { retval.Add(Flags.FromParameterInfo(parameterInfo)); } return retval.ToArray(); }
/// <summary> /// Runs the specified example name. /// </summary> /// <param name="exampleName">Name of the example.</param> /// <param name="session">The session.</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, GoogleAdsClient session, IEnumerable <string> args) { ExampleBase codeExample = null; string[] splits = exampleName.Split('.'); string exampleNameWithoutVersion = null; if (splits.Length == 2) { exampleNameWithoutVersion = splits[1]; } if (codeExampleMap.ContainsKey(exampleName)) { codeExample = codeExampleMap[exampleName]; } else if (!string.IsNullOrEmpty(exampleNameWithoutVersion) && codeExampleMap.ContainsKey(exampleNameWithoutVersion)) { codeExample = codeExampleMap[exampleNameWithoutVersion]; } if (codeExample != null) { Console.WriteLine($"Requested: '{exampleName}', Loaded: '{codeExample.VersionedName}'."); } else { throw new KeyNotFoundException($"Code example not found: '{exampleName}'."); } Console.WriteLine(codeExample.Description); Flag[] flags = GetFlagsFromRunMethodSignature(codeExample); if (flags.Length > 0) { Flags.Parse(args, flags); } object[] paramValues = BuildExampleParameterList(session, flags); MethodInfo runMethod = GetRunMethod(codeExample); runMethod.Invoke(codeExample, paramValues); }
/// <summary> /// Runs the examples from command line arguments. /// </summary> /// <param name="args">The command line arguments.</param> /// <returns>The application's exit code. The valid return codes are: /// /// <list type="bullet"> /// <item><description>0. The code example ran successfully.</description></item> /// <item><description>1. The code example threw an exception and did not complete /// successfully.</description></item> /// <item><description>2. The application was invoked with an incorrect command line /// argument.</description></item> /// </list> /// </returns> private static int RunExamplesFromCommandLineArguments(string[] args) { ExampleRunner runner = new ExampleRunner(); runner.LoadCodeExamples(Assembly.GetExecutingAssembly()); if (args.Length == 0) { // Bad command line parameter. ShowUsage(runner); return(2); } string exampleName = args[0]; try { runner.Run(exampleName, args.Skip(1)); return(0); } catch (Exception e) when(e is KeyNotFoundException) { // First arg is not a valid example name. Console.WriteLine("First arg is not a valid example name"); ShowUsage(runner); return(2); } catch (Exception e) when(e is ArgumentException) { // Args passed to runner are invalid Console.WriteLine(e.Message); Console.WriteLine(ExampleBase.GetUsage(runner.GetCodeExampleType(exampleName))); return(2); } catch (Exception e) { // Indicates a failure due to an unhandled exception. Console.WriteLine(e.Message); return(1); } }
/// <summary> /// Runs the specified example name. /// </summary> /// <param name="exampleName">Name of the example.</param> /// <param name="session">The session.</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, GoogleAdsClient session, IEnumerable <string> args) { ExampleBase codeExample = null; if (!codeExampleMap.TryGetValue(exampleName, out codeExample)) { throw new KeyNotFoundException($"Example not found: {codeExample}"); } Console.WriteLine(codeExample.Description); Flag[] flags = GetFlagsFromRunMethodSignature(codeExample); if (flags.Length > 0) { Flags.Parse(args, flags); } object[] paramValues = BuildExampleParameterList(session, flags); MethodInfo runMethod = GetRunMethod(codeExample); runMethod.Invoke(codeExample, paramValues); }
/// <summary> /// Gets the Run method for a code example. /// </summary> /// <param name="codeExample">The code example.</param> /// <returns>The <see cref="MethodInfo"/> object for the <code>Run</code> method.</returns> private static MethodInfo GetRunMethod(ExampleBase codeExample) { return(codeExample.GetType().GetMethod("Run")); }
/// <summary> /// Registers the code example. /// </summary> /// <param name="key">The code example name.</param> /// <param name="value">The code example instance.</param> private static void RegisterCodeExample(string key, ExampleBase value) { codeExampleMap.Add(new ExamplePair(key, value)); }
/// <summary> /// Invokes the run method. /// </summary> /// <param name="codeExample">The code example.</param> /// <param name="session">The GoogleAdsClient session for running the code example.</param> private static void InvokeRun(ExampleBase codeExample, GoogleAdsClient session) { MethodInfo methodInfo = codeExample.GetType().GetMethod("Run"); methodInfo.Invoke(codeExample, GetParameters(session, codeExample)); }