[英]Non-static method requires a target C#
我有一個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 the method.
if (testListBox.InvokeRequired)
testListBox.BeginInvoke(new DelegateForTest(functionForTestListBox));
else
functionForTestListBox();
}
public void functionForTestListBox()
{
_t = testListBox.SelectedIndex;
if (_t < 0)
return;
_v = testListBox.SelectedValue;
method = _v as MethodInfo;
if (method == null)
return;
_selectedMethod = method.Name;
MessageBox.Show(_selectedMethod.ToString());
method.Invoke(null, null);//<----Not sure about this. it runs fine when I dont invoke in a thread.
counter++;
}
private void runTestButton_Click(object sender, EventArgs e)
{// Click event that calls the selected method in the thread
if (_serverStatus == "Running")
{
if (_testStatus == "Not Running")
{
// create Instance new Thread and add function
// which will do some work
try
{
SetupTestEnv();
//functionForOutputTextBox();
Thread UIthread = new Thread(new ThreadStart(GetTest));
UIthread.Name = "UIThread";
UIthread.Start();
// Update test status
_testStatus = "Running";
//Make thread global
_UIthread = UIthread;
}
catch
{
MessageBox.Show("There was an error at during the test setup(Note: You must install each web browser on your local machine before attempting to test on them).");
}
}
else
{
MessageBox.Show("Please stop the current test before attempt to start a new one");
}
}
else
{
MessageBox.Show("Please make sure the server is running");
}
}
您試圖在不提供對象實例引用的情況下調用非靜態方法,應該為此方法調用此方法。 由於您正在使用MainForm
類的方法,因此您應該在MethodInfo.Invoke(Object, Object[])
的第一個參數中提供MainForm
類型的MethodInfo.Invoke(Object, Object[])
,在您的情況下:
if(method.IsStatic)
method.Invoke(null, null);
else
method.Invoke(this, null);
在單獨的線程上執行方法的示例:
public MethodInfo GetSelectedMethod()
{
var index = testListBox.SelectedIndex;
if (index < 0) return;
var value = testListBox.SelectedValue;
return value as MethodInfo;
}
private void ThreadProc(object arg)
{
var method = (MethodInfo)arg;
if(method.IsStatic)
method.Invoke(null, null)
else
method.Invoke(this, null);
}
private void RunThread()
{
var method = GetSelectedMethod();
if(method == null) return;
var thread = new Thread(ThreadProc)
{
Name = "UIThread",
};
thread.Start(method);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.