2014-11-21 76 views
2

有沒有更好的方式來通過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(); 
    } 
} 

如果有人有任何建議,這將幫助我,我將不勝感激。

+2

['開關(){}'](http://msdn.microsoft.com/en-us/library/06tc147t.aspx)也許 – 2014-11-21 14:18:06

+0

或字典=>動作字典。 – 2015-07-31 16:59:59

回答

1

我的事情下面型號供您也許代碼好:

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(); 
}