Using reflection


Author
Message
Ross L. Rooker, Sr.
Ross L. Rooker, Sr.
StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)
Group: Forum Members
Posts: 153, Visits: 462
I have a reference in my project to a Sharpshooter Report. When I instantiate it, using the name of the the control "ListReportControl" in the second line below works, but I need to change that line to be able to specify a string variable that has the value of the report control. How would the line need to be changed to allow this?

string strReportName = "ListReportControl";

Control newSample = (Control)Activator.CreateInstance(typeof(ListReportControl));

Reply
Ross L. Rooker, Sr.
Ross L. Rooker, Sr.
StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)StrataFrame User (245 reputation)
Group: Forum Members
Posts: 153, Visits: 462
{
    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);

}

 


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search