我可以使用反射通过提供现有变量的名称来获取该变量吗?

发布于 2024-11-13 06:51:02 字数 1522 浏览 1 评论 0原文

我正在接管某人的工作,并且有很多重复的代码。现在,我只想更改以下代码:(我想要更改的代码位于该代码块之后)

        if (Session["opt3PSRAddHrs4"] != null)
        {
            lblDay4AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs4"];
        }
        else
        {
            lblDay4AddHrs.Visible = false;
        }



        if (Session["opt3PSRAddHrs5"] != null)
        {
            lblDay5AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs5"];
        }
        else
        {
            lblDay5AddHrs.Visible = false;
        }

        if (Session["opt3PSRAddHrs6"] != null)
        {
            lblDay6AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs6"];
        }
        else
        {
            lblDay6AddHrs.Visible = false;
        }

        if (Session["opt3PSRAddHrs7"] != null)
        {
            lblDay7AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs7"];
        }
        else
        {
            lblDay7AddHrs.Visible = false;
        }

for (int i = 0; i < 7; i++) {

   Label label = Reflection.getVariable(type = "Label", name = "lblDay" + i + "AddHrs");
   string sessionData = (string) Session["opt3PSRAddHrs" + i];
   if ( sessionData != null) {
      label.Text = "Additional Hours: " + sessionData;
   }
   else {
      label.Visible = false;
   }
}

使用反射。既然这些标签的名称遵循一定的模式,那么反射有帮助吗?

(顺便说一句,也许将所有 opt3PSRAddHrs 内容放入一个数组中是一个好主意,但你知道,现在我不想更改那部分代码......每次更改都可能会留下不一致......)

I'm taking over somebody's work and there is a lot of duplicated code. For now, I just want to change the following code: (the code I wanted to change is after this block of code)

        if (Session["opt3PSRAddHrs4"] != null)
        {
            lblDay4AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs4"];
        }
        else
        {
            lblDay4AddHrs.Visible = false;
        }



        if (Session["opt3PSRAddHrs5"] != null)
        {
            lblDay5AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs5"];
        }
        else
        {
            lblDay5AddHrs.Visible = false;
        }

        if (Session["opt3PSRAddHrs6"] != null)
        {
            lblDay6AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs6"];
        }
        else
        {
            lblDay6AddHrs.Visible = false;
        }

        if (Session["opt3PSRAddHrs7"] != null)
        {
            lblDay7AddHrs.Text = "Additional Hours: " + (String)Session["opt3PSRAddHrs7"];
        }
        else
        {
            lblDay7AddHrs.Visible = false;
        }

to

for (int i = 0; i < 7; i++) {

   Label label = Reflection.getVariable(type = "Label", name = "lblDay" + i + "AddHrs");
   string sessionData = (string) Session["opt3PSRAddHrs" + i];
   if ( sessionData != null) {
      label.Text = "Additional Hours: " + sessionData;
   }
   else {
      label.Visible = false;
   }
}

using reflection. Since the name of those labels follow a pattern, can reflection help?

(BTW, perhaps putting all opt3PSRAddHrs stuff in an array is a good idea, but you know, for now I don't want to change that part of code... Every change will probably leave inconsistency...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-11-20 06:51:02

反思不是答案。您应该使用 Page.FindControl 方法反而。

要在页面级别查找标签,您可以使用:

Label label = (Label)FindControl("lblDay" + i + "AddHrs");

请注意,您需要在保存标签的容器上使用它。例如,如果您的标签存在于带有 ID="myPanel"Panel 中,您将使用 myPanel.FindControl(...)

Reflection isn't the answer. You should use the Page.FindControl method instead.

To find a label at the page level you would use:

Label label = (Label)FindControl("lblDay" + i + "AddHrs");

Note that you'll need to use it on the container which holds your labels. For example, if your labels exist within a Panel with ID="myPanel" you would use myPanel.FindControl(...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文