C# 调用列表框中选定的方法
我正在创建的Windows窗体应用程序有问题。该应用程序应该是一个集成测试应用程序。本质上,它应该测试在我的一个类中使用大量web服务的方法。我从我想通过反射测试的类中加载方法,这样做:C# 调用列表框中选定的方法,c#,multithreading,reflection,methods,runtime,C#,Multithreading,Reflection,Methods,Runtime,我正在创建的Windows窗体应用程序有问题。该应用程序应该是一个集成测试应用程序。本质上,它应该测试在我的一个类中使用大量web服务的方法。我从我想通过反射测试的类中加载方法,这样做: private List<string> GetMethods(Type type) { return (from method in type.GetMethods() where method.IsPublic && method.ReturnType =
private List<string> GetMethods(Type type)
{
return (from method in type.GetMethods() where method.IsPublic &&
method.ReturnType == typeof(void) select method.Name).ToList();
}
这甚至不会编译,因为ThreadStart需要一个方法名作为参数。有什么办法可以做到这一点吗?也许我在逻辑上是错的,但我想为每个需要运行的方法创建一个线程,并执行该方法,不管用户指定多少次。这应该是一种进行集成测试以及一些负载测试的方法,以查看web服务可以处理什么 您可以使用类似的方法来获得所需的方法:
private List<MethodInfo> GetMethods(Type type)
{
return (from method in type.GetMethods()
where method.IsPublic &&
method.ReturnType == typeof(void)
select method).ToList();
}
或者您可以这样写(只有在方法完全没有参数的情况下,注意继承的方法可能会有参数!):
如果方法采用参数,则必须相应地传递对象数组(object[]{…}
),而不是null
(在当前MethodInfo
调用的Invoke
方法中);当然,使用将更正阵列中的对象
实际上,如果您获取一个线程列表,并为列表中的每个MethodInfo添加一个新线程,这样您就可以在之后控制它们(比如如果您想停止一个线程)。HashMap也是一个不错的选择,键是MethodInfo或Action,值是关联的线程。您可以使用类似的方法来获得所需的方法:
private List<MethodInfo> GetMethods(Type type)
{
return (from method in type.GetMethods()
where method.IsPublic &&
method.ReturnType == typeof(void)
select method).ToList();
}
或者您可以这样写(只有在方法完全没有参数的情况下,注意继承的方法可能会有参数!):
如果方法采用参数,则必须相应地传递对象数组(object[]{…}
),而不是null
(在当前MethodInfo
调用的Invoke
方法中);当然,使用将更正阵列中的对象
实际上,如果您获取一个线程列表,并为列表中的每个MethodInfo添加一个新线程,这样您就可以在之后控制它们(比如如果您想停止一个线程)。HashMap也是一个不错的选择,键是MethodInfo或Action,值是关联的线程。您可以实现一个包含MethodInfo和方法名称的私有字典
private Dictionary<string, MethodInfo> methodList;
private List<string> GetMethods(Type type)
{
methodList = new Dictionary<string, MethodInfo>();
type.GetMethods().Where(m=>m.IsPublic && m.ReturnType.Equals(typeof(void))).ToList().ForEach(m=>
methodList.Add(m.Name,m)
);
return methodList.Keys.Select(k => k).ToList();
}
私有字典方法列表;
私有列表GetMethods(类型)
{
methodList=新字典();
type.GetMethods().Where(m=>m.IsPublic&&m.ReturnType.Equals(typeof(void)).ToList().ForEach(m=>
方法列表。添加(m.Name,m)
);
返回methodList.Keys.Select(k=>k.ToList();
}
选择下拉列表后,您可以在字典中找到方法并执行它。您可以实现一个私有字典,其中包含MethodInfos和方法名称
private Dictionary<string, MethodInfo> methodList;
private List<string> GetMethods(Type type)
{
methodList = new Dictionary<string, MethodInfo>();
type.GetMethods().Where(m=>m.IsPublic && m.ReturnType.Equals(typeof(void))).ToList().ForEach(m=>
methodList.Add(m.Name,m)
);
return methodList.Keys.Select(k => k).ToList();
}
私有字典方法列表;
私有列表GetMethods(类型)
{
methodList=新字典();
type.GetMethods().Where(m=>m.IsPublic&&m.ReturnType.Equals(typeof(void)).ToList().ForEach(m=>
方法列表。添加(m.Name,m)
);
返回methodList.Keys.Select(k=>k.ToList();
}
选择下拉列表后,您可以在字典中找到方法并执行它。您可以使用
Activator
创建类的实例
然后可以使用Invoke
调用它的一个方法
像这样的方法应该会奏效:
private void RunMethods(Type type)
{
foreach( var item in lstMethodList.SelectedItems )
{
foreach( var method in type.GetMethods() )
{
if( String.Equals( item.ToString(), method.Name))
{
MethodInfo capturedMethod = method;
var t = new Thread( () => ThreadMain( type, capturedMethod ) );
t.Start();
}
}
}
}
static void ThreadMain( Type type, MethodInfo mi )
{
object o = Activator.CreateInstance( type );
object[] parameters = new object[] { };
mi.Invoke( o, parameters );
}
我假设正在测试的类有一个无参数构造函数。您可以使用
激活器创建类的实例
然后可以使用Invoke
调用它的一个方法
像这样的方法应该会奏效:
private void RunMethods(Type type)
{
foreach( var item in lstMethodList.SelectedItems )
{
foreach( var method in type.GetMethods() )
{
if( String.Equals( item.ToString(), method.Name))
{
MethodInfo capturedMethod = method;
var t = new Thread( () => ThreadMain( type, capturedMethod ) );
t.Start();
}
}
}
}
static void ThreadMain( Type type, MethodInfo mi )
{
object o = Activator.CreateInstance( type );
object[] parameters = new object[] { };
mi.Invoke( o, parameters );
}
我假设被测试的类有一个无参数构造函数。谷歌搜索没有帮助吗?您尝试调用的是静态方法还是实例方法?它们都是我尝试调用的实例方法。我开始认为我做得不对,因为我从中得到方法的类是测试我想要测试的类的类。。。因此,也许我应该从需要进行自身测试的类中检索方法。谷歌搜索没有帮助吗?您尝试调用的是静态方法还是实例方法?它们都是我尝试调用的实例方法。我开始认为我做得不对,因为我从中得到方法的类是测试我想要测试的类的类。。。因此,也许我应该从需要自己测试的类中检索方法。这看起来很有希望,但我已经更改了代码,在运行时出现了一个错误“error binding to target method”。这可能是我在其他地方的代码中的错误,而不是您的逻辑。您假设这些方法是静态的。这可能是因为您正在测试的方法有参数,而如果您使用这段代码,它们不应该有参数,因为我们在这里使用的是操作类型。编辑@NicholasButler是正确的。感谢@NicholasButler,我在我的答案中添加了一段代码。您的代表中有一个。您需要在循环中添加一个tempmi
。这看起来很有希望,但我已经这样修改了代码,在运行时出现了一个错误“error binding to target method”。这可能是我在其他地方的代码中的错误,而不是您的逻辑。您假设这些方法是静态的。这可能是因为您正在测试的方法有参数,而如果您使用这段代码,它们不应该有参数,因为我们在这里使用的是操作类型。编辑@NicholasButler是正确的。感谢@NicholasButler,我在我的答案中添加了一段代码。您的代表中有一个。您需要在循环中添加一个tempmi
。是的,类