0

有没有办法访问通过参数的变量名传递给方法的参数?

btn_Click(object sender, EventArgs e)
{
    SortedList<int, string> paygrades = new SortedList<int, string>();
    populatePaygrades(paygrades, sqlconnection);
    doStuffWithLists(paygrades);
}

protected void doStuffWithLists(params SortedList<int,string>[] lists)
{
 doStuffWithSubList(lists.paygrades) //??? how can I access it similar to this?
}

提前感谢您提供的任何帮助。

4

2 回答 2

2

不可能,paygrades名称在函数中是本地的btn_click。除非SortedList不包含名为 paygrades 的字段(我向您保证不包含),否则您将不会完成任何事情。

如果要按名称访问子列表,可以扩展 SortedList 添加名称字段并按名称搜索第一个。

扩展排序列表:

class MySortedList: SortedList {
    private string name;
    public string Name{
        get { return this.name; }
        set { this.name=value; }
    }

    //you then need to wrap all the constructors you need from SortedList
}

在您的代码中使用它

protected void doStuffWithLists(params MySortedList<int,string>[] lists){
    foreach(MySortedList l in lists){
        if(l.Name == "paygrades")
             doStuffWithSubList(l);
}
于 2012-09-18T14:17:05.067 回答
2

你不能。传递给您的方法的变量名称在方法中不可用。

编辑:根据您在其他地方的评论,我显然对您尝试做的事情有错误的印象。您可以传递字典,而不是使用名称来扩充 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)
{
   ...
}
于 2012-09-18T14:19:21.897 回答