如何通过传递的变量名访问params

时间:2012-09-18 14:11:13

标签: c# .net

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

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?
}

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

2 个答案:

答案 0 :(得分:2)

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

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

扩展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);
}

答案 1 :(得分: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)
{
   ...
}