我有以下用于创建 web 控件的方法(现在只是文字和面板)
private T CreateControl<T>(string s1, string s2, string m1)
where T: WebControl , new()
{
T ctrl = default(T);
if (ctrl is Literal )
{
Literal l = new Literal();
l.ID = s1 + "ltl" + s2;
l.Text = s1 + " " + s2;
ctrl = (Literal)l;
}
else if (ctrl is Panel)
{
Panel p = new Panel();
p.ID = s1+ "pnl" + s2;
p.CssClass = s1 + "graphs";
p.Attributes.Add("responsefield", s2);
p.Attributes.Add("metricid", m1);
ctrl = (Panel)l;
}
else if (ctrl is CheckBox)
return ctrl;
}
我将其调用为:
Literal lg = CreateControl<Literal>("emails", name.Name, name.MetricId.ToString() );
但转换失败:
ctrl = (Literal)l;
ctrl = (Panel)p;
错误:
Cannot implicitly convert type 'System.Web.UI.WebControls.Panel' to 'T'
有人可以建议如何处理这种转换吗?
提前致谢...