是否有更好的方法将其通过addClick传递(理想情况下,我根本不需要它,而我希望它自动传递)?

public void addClick(object sender, EventArgs e)
{
    if ((string) HttpContext.Current.Session["whichMenu"] == "systemDateFormats")
    {
        WorldViewNet.system.DateFormats dateformats = new WorldViewNet.system.DateFormats();
        dateformats.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingLabels")
    {
        WorldViewNet.programming.Labels labels = new WorldViewNet.programming.Labels();
        labels.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingPLUSearch")
    {
        WorldViewNet.programming.PLUSearch pluSearch = new WorldViewNet.programming.PLUSearch();
        pluSearch.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingServings")
    {
        WorldViewNet.programming.Servings servings = new WorldViewNet.programming.Servings();
        servings.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingShops")
    {
        WorldViewNet.programming.Shops shops = new WorldViewNet.programming.Shops();
        shops.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingTextsSearch")
    {
        WorldViewNet.programming.TextsSearch textsSearch = new WorldViewNet.programming.TextsSearch();
        textsSearch.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "systemTemplates")
    {
        WorldViewNet.system.Templates templates = new WorldViewNet.system.Templates();
        templates.addClick();
    }
}


如果有人有任何建议,那将对我有所帮助,我将不胜感激。

最佳答案

我认为以下模型可能对您的代码有利:

public void addClick(object sender, EventArgs e)
{
    object control;
    string opt = (string) HttpContext.Current.Session["whichMenu"];

    switch (opt)
    {
        case "systemDateFormats": control = new WorldViewNet.system.DateFormats();
            break;
        case "programmingLabels": control = new WorldViewNet.programming.Labels();
            break;
        case "programmingPLUSearch": control = new WorldViewNet.programming.PLUSearch();
            break;
        case "programmingServings": control = new WorldViewNet.programming.Servings();
            break;
        case "programmingShops": control = new WorldViewNet.programming.Shops();
            break;
        case "programmingTextsSearch": control = new WorldViewNet.programming.TextsSearch();
            break;
        case "systemTemplates": control = new WorldViewNet.system.Templates();
            break;
        default: new WorldViewNet.system.DefaultType();
    }

    ((dynamic)control).addClick();
}

关于c# - 添加点击if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27063310/

10-12 19:15