我想根據查找表中的值調用方法。這些值將在數據庫中查找,並將迭代完成。我試圖避免的是:根據查找表中的數據運行函數
foreach (Row r in rows)
{
if (r["command"] == "Command1")
MyClass.Command1();
else if (r["command"] == "Comman2")
MyClass.Command2();
else if (r["command"] == "Comman3")
MyClass.Command3();
}
這是我要支持遺留代碼,但我知道肯定有一個更好的方式來做到這一點。目前代碼看起來像上面,但我正在尋找一個更優雅的解決方案。
編輯:
基於下面的建議,我試圖做這樣的事情:
static void Main(string[] args)
{
Dictionary<string, Action<MyClass>> myActions = new Dictionary<string,Action<MyClass>>();
myActions.Add("Command1",MyClass.DoCommand1("message1"));
myActions.Add("Command2",MyClass.DoCommand1("message2"));
myActions["Command1"]();
}
與我的類文件看起來像這樣:
public class MyClass
{
public void DoCommand1(string message)
{
Console.WriteLine(message);
}
public void DoCommand2(string message)
{
Console.WriteLine(message);
}
}
然而,我我得到語法錯誤,指出非靜態字段,方法或屬性MyClass.DoCommand1(string)需要對象引用。有任何想法嗎?
請注意我正在使用.NET 2.0框架。
我喜歡這個解決方案很多,但我不知道如何申報班級內的代表。你能解釋一下嗎? – 2009-12-23 22:49:31
委託類型爲Action,只需聲明一個Dictionary並用YourClass.MethodName填充它,則不需要執行委託實例化,編譯器應該處理該問題。 –
albertein
2009-12-23 22:54:27
你能看看我上面的編輯,讓我知道我要去哪裏錯了嗎?我使用的是.NET 2.0框架,我不相信Action會一直持續到3.0。 – 2009-12-23 23:10:28