我有一个win表单应用程序,其中列表框显示方法(按属性).我试图动态调用线程中的方法,使用反射从列表框的选定值获取方法信息.但是,当调用Methodinfo.Invoke时,我得到了这个内部异常"非静态方法需要一个目标C#".
这是我的代码(请记住,我仍然是c#和编程的新手.)
private void PopulateComboBox()
{//Populates list box by getting methods with declared attributes
MethodInfo[] methods = typeof(MainForm).GetMethods();
MyToken token = null;
List<KeyValuePair<String, MethodInfo>> items =
new List<KeyValuePair<string, MethodInfo>>();
foreach (MethodInfo method in methods)
{
token = Attribute.GetCustomAttribute(method,
typeof(MyToken), false) as MyToken;
if (token == null)
continue;
items.Add(new KeyValuePair<String, MethodInfo>(
token.DisplayName, method));
}
testListBox.DataSource = items;
testListBox.DisplayMember = "Key";
testListBox.ValueMember = "Value";
}
public void GetTest()
{//The next two methods handle selected value of the listbox and invoke …Run Code Online (Sandbox Code Playgroud)