I have an issue with a Windows Forms application that I am creating. The application is supposed to be an integration testing application. Essentially, it’s supposed to test the methods that utilize lots of web services in one of my classes. I am loading the methods from the class I want to test via reflection, and am doing so like this:
private List<string> GetMethods(Type type)
{
return (from method in type.GetMethods() where method.IsPublic &&
method.ReturnType == typeof(void) select method.Name).ToList();
}
This returns a list of the methods from that class that have been created to test the web services and places them in a ListBox where the user can select as many methods as he/she likes. My confusion comes in here. What I would like to do is get the methods selected by the user and execute the corresponding method X amount of times (there is a text box for entering the number of times to execute a method on the form as well). I can’t figure out how to execute these methods based on the name of the method I got through reflection. I’ve tried something like this, but I know it’s not right:
private void RunMethods(Type type)
{
var tester = new ClassToTest();
foreach(var item in lstMethodList.SelectedItems)
{
foreach(var method in type.GetMethods())
{
if(String.Equals(item.ToString(), method.Name))
{
ThreadStart ts = new ThreadStart(method.Name);
Thread thread1 = new Thread(ts);
thread1.Start();
}
}
}
}
This won’t even compile, as a ThreadStart requires a method name as a parameter. Is there any way that this is possible to do? Maybe I’m going about it wrong logically, but I’d like to create a thread for each method that needs to be run and execute that method however many times the user specifies. This is supposed to be a way of doing integration testing along with some load testing to see what the web service can handle.
You can create an instance of your class using
Activator.Then you can call one of its methods using
Invoke.Something like this should work:
I’ve assumed the class being tested has a parameter-less constructor.