我正在尝试设置一个dictionary
,然后将其keys
存储items
为listbox
.
我已经能够建立一个dictionary
然后将其keys
输入到 中listbox
,但我不确定如何执行与key
. 从上一个线程有一个建议,但我遇到了问题:原始线程
Dictionary<string, Action> dict = new Dictionary<string, Action>();
public void SetDictionary()
{
//add entries to the dictionary
dict["cat"] = new Action(Cat);
dict["dog"] = new Action(Dog);
//add each dictionary entry to the listbox.
foreach (string key in dict.Keys)
{
listboxTest.Items.Add(key);
}
}
//when an item in the listbox is double clicked
private void listboxTest_DoubleClick(object sender, EventArgs e)
{
testrun(listboxCases.SelectedItem.ToString());
}
public void testrun(string n)
{
//this is supposed to receive the item that was double clicked in the listbox, and run it's corresponding action as defined in the dictionary.
var action = dict[n] as Action action();
}
我相信我上面的代码大部分是正确的并且我理解它,但是行动路线:
var action = dict[n] as Action action();
显示一个错误,指出“操作”需要一个';'
. 我这里的逻辑准确吗?如果是这样,为什么动作调用不正确?