我可以使用反射通过提供现有变量的名称来获取该变量吗?
我正在接管某人的工作,并且有很多重复的代码。现在,我只想更改以下代码:(我想要更改的代码位于该代码块之后)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反思不是答案。您应该使用
Page.FindControl
方法反而。要在页面级别查找标签,您可以使用:
请注意,您需要在保存标签的容器上使用它。例如,如果您的标签存在于带有
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:
Note that you'll need to use it on the container which holds your labels. For example, if your labels exist within a
Panel
withID="myPanel"
you would usemyPanel.FindControl(...)
.