{
string LcClassName = "ListReportControl"; //-- The type will be 'string' in this case
object loObject;
Type loType;
//-- Get the type
loType = Type.GetType(LcClassName);
//-- Create the object
loObject = Activator.CreateInstance(loType); //-- The activator won´t be able to create an instace from a string type
} Yes, this is the dilema which I am trying to solve whether with the above code or from the code at the top of this thread. Basically I need to know the .net syntax to have the Type.GetType() get the type of the control which is represented by the string LcClassName which in the sample above would be "ListReportControl". ListReportControl is a control. One of about 600 report controls. Each control represents a Sharpshooter report. Initially all the Reports are in SQL, then loaded into a TreeView on the left side of a "viewer" form. The user selects a report such as one "ListReportControl". I then need to instantiate that control into a "commonControl" for the global report viewer which appears in a host panel to the right of the treeview. Other than the treeview which comes from a SQL table, I do not want to hardcode all 600 possible report controls into code. Hence when the user clicks a report in the treeview, after the treeView_AfterSelect event is triggered. At that point I have a string value (LcClassName) which has the name of the report control such as "ListReportControl".
Basically inserting this code works perfect with the issue that the Report Name control "ListReportControl" is coded to make it work. I believe there is a way with "reflection" to allow me to specify a string such as LcClassName and the code would then take the "value" of the string and return the "type" of value of the string which is "ListReportControl".
Control
newSample = (Control)Activator.CreateInstance(typeof(ListReportControl));Text =
"Preparing data for report...";(newSample
as IReportControl).PrepareData();Text =
"Rendering...";activeReportSlot = (newSample
as IReportControl).GetReportSlot();RenderReport();
newSample.Dock =
DockStyle.Fill;Control
c = null;if (hostPanel.Controls.Count != 0)c = hostPanel.Controls[0];
if (c != newSample){
if (c != null)hostPanel.Controls.Remove(c);
hostPanel.Controls.Add(newSample);
}