你不能。传递给您的方法的变量名称在方法中不可用。
编辑:根据您在其他地方的评论,我显然对您尝试做的事情有错误的印象。您可以传递字典,而不是使用名称来扩充 SortedList。
public void CallsDoStuffWithLists()
{
SortedList<int, string> theFirstList = new SortedList<int, string>();
SortedList<int, string> aSecondList = new SortedList<int, string>();
SortedList<int, string> thirdList = new SortedList<int, string>();
SortedList<int, string> theLastList = new SortedList<int, string>();
PopulateTheFirstList(theFirstList);
PopulateTheSecondList(aSecondList);
//etc
// call do stuff with lists.
DoStuffWithLists(new Dictionary<string, SortedList<int, string>>{{"theFirstList", theFirstList}, {"aSecondList",aSecondList}, {"thirdList", thirdList}, {"theLastList", theLastList}});
}
public void DoStuffWithLists(Dictionary<string, SortedList<int,string>> lists)
{
// does not loop through all,
// does not throw exceptions..
// if there is a list that was misnamed, it will not be handled.
SortedList<int, string> temp;
if(lists.TryGetValue("theFirstList", out temp)) DoStuffWithSubList(temp);
if(lists.TryGetValue("aSecondList", out temp)) DoStuffWithSubList(temp);
// loops through each and acts on them accordingly.
// if DoStuffWithLists is
foreach (var list in lists)
{
//or use switch statement.
if(list.Key == "theFirstList")
{
DoStuffWithSubList(list.Value);
}
else if(list.Key == "aSecondList")
{
DoOtherStuffWithSublist(list.Value);
}
//etc...
else
{
//we got an unexpected list, what do we do with it?
}
}
附带说明:虽然我已经向您展示了如何做您想做的事,但这并不意味着这是一个好主意。在这种情况下,您似乎在增加复杂性而没有任何收获。您有更复杂且容易出错的代码,尽管您将数据检索与每个列表的数据处理分开,但您正在组合所有列表的数据处理。
您是否考虑过让 DoStuffWithLists 实际上也进行数据检索?更不容易出错,无需匹配名称。
btn_Click(object sender, EventArgs e)
{
DoStuffWithLists();
}
public void DoStuffWithLists()
{
//this is far simpler and less error prone.
SortedList<int, string> theFirstList = new SortedList<int, string>();
PopulateTheFirstList(theFirstList);
DoStuffWithSubList(theFirstList);
SortedList<int, string> aSecondList = new SortedList<int, string>();
PopulateTheSecondList(aSecondList);
DoOtherStuffWithSublist(aSecondList);
}
或者更好的是仍然有一个方法来完全处理每个 List 并调用它......
btn_Click(object sender, EventArgs e)
{
//this is simpler yet, separates the concerns of each type of list.
HandlePayGrade();
HandleSecondList();
HandleThirdList();
}
public void HandlePayGrade()
{
// you are still separating your data access and processing concerns here.
SortedList<int, string> paygrades = new SortedList<int, string>();
populatePaygrades(paygrades, sqlconnection);
DoStuffWithPaygrades(paygrades);
}
编辑:下面的原始答案-
根据您命名(列表)的方式,您似乎希望同时处理传递给 doStuffWithLists 的所有列表。情况并非如此,每次调用它都会处理一个已通过的列表。
使用整数的示例...
//Double an int is invoked 3 times, each time dealing with one integer.
public int DoubleAnInt(int x)
{
return x+x;
}
public void CallsDoubleAnInt()
{
int a = 1;
int b = DoubleAnInt(a);
int c = DoubleAnInt(b);
int d = DoubleAnInt(c);
}
看起来你正试图让 doStuffWithLists 根据它被调用的位置而表现不同。在这种情况下,您只需要不同的方法。
protected void doStuffWithPaygradeLists(SortedList<int,string> list)
{
...
}
protected void doStuffWithSomeOtherLists(SortedList<int,string> list)
{
...
}