【发布时间】:2021-10-01 06:48:56
【问题描述】:
我想把一个控制方法的输出串起来; 这是我的代码,但它不起作用;
public static string get(string control_name, string method)
{
string result = "null";
foreach (var control_ in Controls) //Foreach a list of contrls
{
if ((string)control_.Name == (string)control_name) //Find the control
{
try
{
Type type = control_.GetType();
MethodInfo method_ = type.GetMethod(method);
result = (string)method_.Invoke(method, null);
//Ejecuting and savind the output of the method on a string
}
catch (Exception EXCEPINFO)
{
Console.WriteLine(EXCEPINFO);
}
}
}
return result;
}
调用函数:
form.Text = get("button_1", "Text");
非常感谢您
【问题讨论】:
-
文本是一个属性。所以你不需要
GetMethod,而是GetProperty -
MethodInfo.Invoke的第一个参数是你调用方法的对象,所以这里control_。
标签: c# list winforms methods controls