我想將一個參數傳遞給一個方法,然後使用該參數作爲數組擴展方法,但我掙扎着。我的代碼是:是否有可能使用方法參數作爲數組擴展方法
//create method
public static void BankChoice(string SearchItem)
{
//declare variables
double tempMin = 0;
int minIndex = 0;
//set a temporary double as the first index of array
tempMin = Program.array_SH1[0].SearchItem;
//start loop to go through whole array
for (int y = 0; y <= array_SH1.Length; y++)
{
//if the temp double is bigger than the array item,
//make array item temp double
if (tempMin > array_SH1[y].SearchItem)
{
tempMin = array_SH1[y].SearchItem;
minIndex = y;
}
}
}
我會再調用代碼:
BankChoice("OpenPrice")
然而,這是行不通的。編譯器不會接受字符串作爲數組擴展名,它只是拋出錯誤。 反正是有解決這個問題,而無需手寫做到這一點,併爲SearchItem的所有變體創建方法
感謝
不,你不能這樣做。你需要研究的是如何使用反射來實現這一點。 – Shoe
備註:您的示例不顯示類似於任何地方的調用擴展方法的語法... –