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 ??