using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
//using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace Record
{
	/// <summary>
	/// Uses reflection to set up test information available in the Assembly.  All classes
	/// containing a public static method named TestProc will be enumerated.
	/// </summary>
	public class MainTest
	{
		/// <summary>
		/// Delegate used to display debug information.
		/// </summary>
		public delegate void DisplayLineDelegate(String txt);
		public DisplayLineDelegate m_showLine = null;

		/// <summary>
		/// Information for each valid test.  Stores the owner class name and
		/// the MethodInfo for each test.
		/// </summary>
		protected class TestInfo
		{
			public string m_name = null;
			public MethodInfo m_methInfo = null;
		}

		/// <summary>
		/// Array of test information.
		/// </summary>
		protected TestInfo[] m_tests = null;

		/// <summary>
		/// Number of tests detected in the Assembly.
		/// </summary>
		public int NumTests { get { return m_numTests; } }
		int m_numTests = 0;

		/// <summary>
		/// Creates an instance of MainTest and fills the list of valid test
		/// procedures.
		/// </summary>
		/// <param name="showLineFunc">Delegate for displaying debug information</param>
		public MainTest(DisplayLineDelegate showLineFunc)
		{
			// Set an arbitrary maximum number of tests
			const int kMaxTests = 32;
			m_numTests = 0;
			m_tests = new TestInfo[kMaxTests];

			// Access all of the Types in the Assembly
			[U][I][B]Type[] asmTypes = Assembly.GetExecutingAssembly().GetTypes();[/B][/I][/U]
			foreach (Type t in asmTypes)
			{
				// Check if the current Type is a class
				if (t.IsClass)
				{
					// Access the methods of the class
					MethodInfo[] methInfo = t.GetMethods();
					foreach (MethodInfo m in methInfo)
					{
						// If the method is named TestProc then it is a candidate
						if (string.Compare(m.Name, "TestProc", false) == 0)
						{
							// Check the parameter information of the method
							ParameterInfo[] args = m.GetParameters();

							// If the method is static, public and takes one parameter
							// then assume it is a test method
							if (m.IsStatic && m.IsPublic && args.Length == 1)
							{
								if (m_numTests == kMaxTests - 1)
									return;

								// Store the test information
								m_tests[m_numTests] = new TestInfo();
								m_tests[m_numTests].m_methInfo = m;
								m_tests[m_numTests].m_name = t.Name;
								m_numTests++;
							}
						}
					}
				}
			}

			// Store the debug delegate
			m_showLine = showLineFunc;
		}

		/// <summary>
		/// Run the specified test method.
		/// </summary>
		/// <param name="index">Index of the test to be run</param>
		public void RunTest(int index)
		{
			// Validate the index
			Debug.Assert(index >= 0 && index < m_numTests);

			// Show Start debug info
			m_showLine(string.Format("****Begin {0}****", m_tests[index].m_name));
			try
			{
				// Run the test
				object[] arg = {m_showLine};
				m_tests[index].m_methInfo.Invoke(this,arg);
			}
			catch (Exception e)
			{
				m_showLine(String.Format("FAILURE: {0}", e.Message));
			}

			// Show End debug info
			m_showLine(String.Format("****End {0}****", m_tests[index].m_name));
			m_showLine("");
		}

		/// <summary>
		/// Get the name of the specified test.
		/// </summary>
		/// <param name="index">Index of the test.</param>
		/// <returns>Name of the class owning the test method.</returns>
		public string GetTestName(int index)
		{
			Debug.Assert(index >= 0 && index < m_numTests);

			return m_tests[index].m_name;
		}
	}
}

This is part of a project.
The project builds without errors !

But when I debug it, it shows an exception :
ReflectionTypeLoadException
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information (on the marked line).

What could be the reason ??

I have attached a snap shot of the Exception dialog box !
You may have a look at it !

In that window (the exception window) click on the "View Detail" link and look at the LoaderException property to see what it says.

In that window (the exception window) click on the "View Detail" link and look at the LoaderException property to see what it says.

This is what the LoaderException property was showing!:

[B]{"Could not load file or assembly 'Microsoft.WindowsCE.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"Microsoft.WindowsCE.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac"}
[/B]

Is the problem related to version number of the included assembly ???

System.Reflection.ReflectionTypeLoadException was unhandled

Message="Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."
  
Source="mscorlib"

StackTrace:
       at System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark)
       at System.Reflection.Assembly.GetTypes()
       at Record.MainTest..ctor(DisplayLineDelegate showLineFunc) in C:\Users\User\Documents\Visual Studio 2008\Projects\Record\Record\MainTest.cs:line 57
       at Record.TestForm.TestForm_Load(Object sender, EventArgs e) in C:\Users\User\Documents\Visual Studio 2008\Projects\Record\Record\TestForm.cs:line 184
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Record.TestForm.Main() in C:\Users\User\Documents\Visual Studio 2008\Projects\Record\Record\TestForm.cs:line 16
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

If I have understood the error correctly, I guess this is the problem !

My computer has Microsoft.WindowsCE.Forms Version : 1.0.5.0.
I guess I need version: 2.0.0.0 !

Could anyone tell where to get that version !????
I could only find .... 1.0.5.0, 1.0.2.0 yet !

This article has been dead for over six months. Start a new discussion instead.