Using reflection


Author
Message
Ross L. Rooker, Sr.
Ross L. Rooker, Sr.
StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 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));

Paul Chase
Paul Chase
Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)Advanced StrataFrame User (576 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
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

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Yup...good answer, Paul. Smile
Ross L. Rooker, Sr.
Ross L. Rooker, Sr.
StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)
Group: Forum Members
Posts: 153, Visits: 462
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. }

Guillermo Vilas
Guillermo Vilas
StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)
Group: StrataFrame Users
Posts: 112, Visits: 2.3K
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. }


------------------------------------------------------------------------
I would like to change the world, but they don´t give me the source code.
MS Windows 7 Ultimate 64-Bit
Intel(R) Core(TM)2 Quad CPU Q9300 2.50 GHz
6.00 GB of RAM, NVIDIA GeForce 9800 GT

MacBook Pro i5 OSX Lion
Ross L. Rooker, Sr.
Ross L. Rooker, Sr.
StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 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);

}

 


Guillermo Vilas
Guillermo Vilas
StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)StrataFrame User (192 reputation)
Group: StrataFrame Users
Posts: 112, Visits: 2.3K
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

------------------------------------------------------------------------
I would like to change the world, but they don´t give me the source code.
MS Windows 7 Ultimate 64-Bit
Intel(R) Core(TM)2 Quad CPU Q9300 2.50 GHz
6.00 GB of RAM, NVIDIA GeForce 9800 GT

MacBook Pro i5 OSX Lion
Ross L. Rooker, Sr.
Ross L. Rooker, Sr.
StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)StrataFrame User (195 reputation)
Group: Forum Members
Posts: 153, Visits: 462
Thanks, I believe this works.
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