我目前已經完成了我的構建腳本並已移至一些C#編程。使用基於在字典中選擇什麼對象的未知方法
這是我有:
public bool resultOfOperation(bool firstOperator, bool secondOperator, string operation)
{
//The bool value in the dictionary is gotten by used the corresponding operation method in the operation class.
Dictionary<string, bool> mappedObjects = new Dictionary<string, bool>();
mappedObjects.Add("OR", new OrOperator().operationOR(firstOperator,secondOperator));
foreach (KeyValuePair<string, bool> keyAndValue in mappedObjects)
{
if (keyAndValue.Key == operation.ToUpper())
return keyAndValue.Value;
}
throw new ArgumentException("Something has gone very wrong.");
}
雖然這個作品,我想其他人做一些事情。我想讓字典保持一個字符串和一個對象。然後我想根據我提供對象的方法使用正確的對象。
例(不工作)
Dictionary<string, object> test = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> keyAndValue in test)
{
if(keyAndValue.Key == "SomethingValue")
keyAndValue.Value.USE METHOD OF CLASS HERE;
}
我應該指出,所有類都會有類似的方法,例如bool FUNCTIONname(布爾運算符)。
我相當肯定我有你匿名和委託,但我不知道如何。我將不勝感激任何幫助。
你能否提供更多的上下文?這個代碼應該解決的實際問題是什麼? – 2010-11-26 11:45:03
啊,是的,這非常含糊。我想要使用詞典中列出的類中的方法。但是因爲我不知道我會得到哪個類,所以我不知道要調用哪個方法名稱。那正是我想要解決的問題。 – Siemsen 2010-11-26 11:46:14