예제 #1
0
        /// <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))
                    {
                        string versionedName = ExampleBase.GetVersionedName(type);
                        string name          = ExampleBase.GetName(type);
                        // Support both versioned and unversioned names.
                        // E.g. You can run GetCampaigns example by either using V5.GetCampaigns
                        // or GetCampaigns.
                        codeExampleMap.Add(versionedName, type);
                        if (codeExampleMap.ContainsKey(name))
                        {
                            Console.WriteLine($"Multiple versions of the code example" +
                                              $" '{name} was found. Using {name} in command " +
                                              $"line will now run {versionedName}. To run a specific" +
                                              $"version, use the example name with a version prefix.");
                        }
                        codeExampleMap.Add(name, type);
                    }
                }
            }
        }
예제 #2
0
        /// <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;
            }
        }