gpt4 book ai didi

c# - 添加点击if语句

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:27 25 4
gpt4 key购买 nike

有没有更好的方法通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com