StrataFrame Forum

Using reflection

http://forum.strataframe.net/Topic17273.aspx

By Ross L. Rooker, Sr. - 6/23/2008

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));

By Paul Chase - 6/24/2008

you just need to call Gettype with the classname and pass that to createinstance.

Dim loObject As Object
Dim loType As Type
  '-- Get the type
 loType = Type.GetType(LcClassName)

 '-- Create the object
 loObject = Activator.CreateInstance(loType)

Paul

By Trent L. Taylor - 6/24/2008

Yup...good answer, Paul. Smile
By Ross L. Rooker, Sr. - 6/24/2008

After converting the VB.NET sample, when run it errors out on the line 9 with Value cannot be null.
Parameter name: type. The problem I believe is the the "type" of the variable LcClassName is "string". The value of LcClassName which is "ListReportControl" is a type of "control". I know in FoxPro you could specify the field as &LcClassName so that the code would use the value rather than the actual field name.

  1. {
  2.     string LcClassName = "ListReportControl";
  3.     object loObject;
  4.     Type loType;
  5.     //-- Get the type
  6.     loType = Type.GetType(LcClassName);
  7.    
  8.     //-- Create the object
  9.     loObject = Activator.CreateInstance(loType);
  10. }
By Guillermo Vilas - 6/24/2008

Hi Ross,

I think the problem is that you´re asking to create an instance of type string as described bellow

  1. {
  2.     string LcClassName = "ListReportControl";  //-- The type will be 'string' in this case
  3.     object loObject;
  4.     Type loType;
  5.     //-- Get the type
  6.     loType = Type.GetType(LcClassName);
  7.    
  8.     //-- Create the object
  9.     loObject = Activator.CreateInstance(loType); //-- The activator won´t be able to create an instace from a string type
  10. }
By Ross L. Rooker, Sr. - 6/24/2008

{
    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);

}

 

By Guillermo Vilas - 6/24/2008

I just red a forum post at http://forums.devx.com/archive/index.php/t-55448.html

basically the string needs to contain both the Assembly name and the class name, separated with comma. Like this:



string lcClassName = "YourAssemblyName, YourReportClassName"



then the rest of the code should work. I haven't tested it.

Good luck Wink
By Ross L. Rooker, Sr. - 6/24/2008

Thanks, I believe this works.