I am loading a dynamically specified user control into a place-holder in the parent (user) control like this:
// dynamically load instance of chosen chart control
string chartClassName = ConfigurationManager.AppSettings["ChartControlClass"];
_chartControl = (IOutputChart)LoadControl(chartClassName + ".ascx");
// add to place-holder
if (chartClassName == "OutputChart_Dundas") phChart.Controls.Add((OutputChart_Dundas)_chartControl);
else if (chartClassName == "OutputChart_Microsoft") phChart.Controls.Add((OutputChart_Microsoft)_chartControl);
else if (chartClassName == "OutputChart_Telerik") phChart.Controls.Add((OutputChart_Telerik)_chartControl);
Clearly it would be nicer not to have to explicitly cast the _chartControl variable each time — is there a cleaner way? Each user control implements the IOutputChart interface; however, I can’t use that directly because Controls.Add() expects a Control object.
Can you not just convert all of these to
Control?