Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Make NDoc compile the code examples contained in your documentation using NLiterate

Rate me:
Please Sign up or sign in to vote.
4.64/5 (19 votes)
26 Apr 20047 min read 123.5K   622   42  
An utility that merges and recompiles the examples in your documentation using NDoc.
using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace NLiterate
{
	/// <summary>
	/// Execution result class.
	/// </summary>
	public class ExecutionResults
	{
		private const int NoOrErrorRetValue = -1;	

		private StringWriter consoleOut = new StringWriter();
		private StringWriter consoleError = new StringWriter();
		private Exception throwedException = null;
		private int returnedValue = NoOrErrorRetValue;

		/// <summary>
		/// Initializes an empty execution result class.
		/// </summary>
		public ExecutionResults()
		{}

		/// <summary>
		/// Gets or sets the console output stream
		/// </summary>
		/// <value>
		/// Console.Out stream.
		/// </value>
		public StringWriter ConsoleOut
		{
			get
			{
				return this.consoleOut;
			}
		}

		/// <summary>
		/// Gets or sets the console error stream
		/// </summary>
		/// <value>
		/// Console.Error stream
		/// </value>
		public StringWriter ConsoleError
		{
			get
			{
				return this.consoleError;
			}
		}

		/// <summary>
		/// Gets a value indicating if the execution has throwed.
		/// </summary>
		/// <value>
		/// true if it has throwed, false otherwise
		/// </value>
		public bool HasThrowed
		{
			get
			{
				return this.throwedException != null;
			}
		}

		/// <summary>
		/// Gets the exception throwed, a null reference if no exception
		/// were throwed.
		/// </summary>
		/// <value>
		/// Throwed <see cref="Exception"/>, a null reference (Nothing in
		/// Visual Basic) if not throwed.
		/// </value>
		public Exception ThrowedException
		{
			get
			{
				return this.throwedException;
			}
		}

		/// <summary>
		/// Gets the execution return value.
		/// </summary>
		/// <value>
		/// integer returned by the entry point method.
		/// </value>
		public int ReturnedValue
		{
			get
			{
				return this.returnedValue;
			}
		}

		/// <summary>
		/// Converts instance to string
		/// </summary>
		/// <returns>
		/// <see cref="String"/> representing the results.
		/// </returns>
		public override string ToString()
		{
			StringWriter sw = new StringWriter();

			sw.WriteLine("Success:\n{0}", !this.HasThrowed);
			sw.WriteLine("Return code:\n{0}",this.returnedValue);
			sw.WriteLine("Console.Out:\n{0}",this.consoleOut.ToString());
			sw.WriteLine("Console.Error:\n{0}",this.consoleError.ToString());
			if (this.HasThrowed)
				sw.WriteLine("Exception:\n{0}",this.throwedException.ToString());

			return sw.ToString();
		}

		/// <summary>
		/// Executes the method <paramref name="mi"/> and returns the result.
		/// </summary>
		/// <param name="mi">Method to execute</param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException">
		/// <paramref name="mi"/> is a null reference (Nothing in Visual Basic).
		/// </exception>
		public static ExecutionResults Execute(MethodInfo mi)
		{
			if (mi==null)
				throw new ArgumentNullException("mi");
			lock(typeof(ExecutionResults))
			{
				TextWriter consoleOut = Console.Out;
				TextWriter consoleError = Console.Error;

				ExecutionResults result = new ExecutionResults();
				Console.SetOut(result.consoleOut);
				Console.SetError(result.consoleError);

				try
				{
					if (mi.ReturnType != typeof(int))
					{
						mi.Invoke(null,null);
						result.returnedValue = NoOrErrorRetValue;
					}
					else
					{
						result.returnedValue = (int)mi.Invoke(null,null);
					}

				}
				catch(Exception ex)
				{
					result.throwedException = ex;
				}
				finally
				{
					Console.SetOut(consoleOut);
					Console.SetError(consoleError);
				}

				return result;
			}
		}

		/// <summary>
		/// Execution results when the entry point method is not found.
		/// </summary>
		/// <returns></returns>
		public static ExecutionResults NoEntryPoint()
		{
			lock(typeof(ExecutionResults))
			{
				ExecutionResults results = new ExecutionResults();
				results.throwedException = new Exception("No entry point found.");
				return results;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions