Error trying to execute reflection "invoke" method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have this code

string theClassName = "Rules.FillListBox.FillListBox" + theType + ", Rules"
Type theClass = Type.GetType(theClassName)
Object o = Activator.CreateInstance(theClass)
object[] parameters = new object[2]
parameters [0] = elListBox
parameters [1] = (IList)Session["AdminPaginaBase"]
theClass.GetMethod("fill").Invoke(o, (object[])parameters)

but, when I try to execute the last line, always obtain this error
"Object type cannot be converted to target type.
The problem, I think, is the session object

Any idea how to solve

TIA, Abel
 
maybe this will help

theClass.GetMethod("fill").Invoke(o, new object[]{elListBox,
(IList)Session["AdminPaginaBase"]});
 
Vladimir S. said:
maybe this will help

theClass.GetMethod("fill").Invoke(o, new object[]{elListBox,
(IList)Session["AdminPaginaBase"]});

I don't see why it would - it's exactly the same code, just in fewer
lines.

To the OP: what are the actual parameters for your method?

If you could post a short but complete example of the problem, that
would help a lot.
 
Rami Saad said:
try casting the "o" value....It might solve the problem

No it won't. How could it? Casting just changes the type of reference,
not (unless there's explicit conversions specified) the type of the
object itself.

As MethodBase.Invoke takes object as its first parameter, it's not
going to care whether or not the reference has been cast first.
 
I need to invoke the unique public method from a inherited class, defined by
string theClassName = "Rules.FillListBox.FillListBox" + theType + ", Rules"
inherited from Rules.FillListBox.FillListBox, the origin class from all inherited classes
This class has only one public method ("fill") with two parameters: first: the Aspx ListBox from this form and the second, the session object from this page, to return the object collection thats fills this page returned from sql server database in objects formats, not DataSet
Is very simple, and I don´t understand where is the error and so, what´s the mistak
Any idea
TIA
Abe
 
Abel said:
I need to invoke the unique public method from a inherited class, defined by:
string theClassName = "Rules.FillListBox.FillListBox" + theType + ", Rules";
inherited from Rules.FillListBox.FillListBox, the origin class from all
inherited classes.
This class has only one public method ("fill") with two parameters:
first: the Aspx ListBox from this form and the second, the session
object from this page

When you say "the session object" - how exactly is the parameter
declared?
to return the object collection thats fills
this page returned from sql server database in objects formats, not
DataSet.

If FillListBox has the method which you wish to call, you don't need
reflection at all in order to call the method - just create the
instance, cast to FillListBox and call the method.
Is very simple, and I don?t understand where is the error and
so, what?s the mistake
Any idea?

Without a complete example, it's hard to say. As ever, if I were you
I'd try to come up with a simple console app which mimics what the
ASP.NET page is trying to do.
 
I need to invoke the unique public method from a inherited class, defined by:
string theClassName = "Rules.FillListBox.FillListBox" + theType + ", Rules";
inherited from Rules.FillListBox.FillListBox, the origin class from all inherited classes.
This class has only one public method ("fill") with two parameters: first: the Aspx ListBox from this form and the second, the session object from this page, to return the object collection thats fills this page returned from sql server database in objects formats, not DataSet.
Is very simple, and I don´t understand where is the error and so, what´s the mistake
Any idea?
TIA.
Abel
 
Abel said:
When I say "the session object" - this is the parameter declaration:

Firs, on Page_Load
IList theList = new ArrayList();
Session["AdminPaginaBase"] = theList;

Second, on invoke:
parameters [1] = (IList)Session["AdminPaginaBase"];

I use polymorphism. Therefore I need to invoke children pages to use
polymorphism benefits. This is this case, I dont invoke FillListBox, I
invoke FillListBoxUserState, a second may be
FillListBoxUserPermissions, a third can be FillListBoxUserRules, but
all inherits form FillListBox and had protected virtuals methods.

But the code you've given doesn't do that - it always tries to call a
method named "fill".

Furthermore, if you have lots of different methods (rather than one
which is overridden) you're *not* using polymorphism.
 
Abel said:
All classes has the same method: fill.

Right. You don't need reflection then (for the invocation).
All classes only can be invoked using the only method: fill.
Right.

The thing is, if I remove the session, and I only have one parameter,
I have'nt errors and always work fine.

I can't pass a session by parameter? This is the dilema.

You should be able to pass whatever you want - but only if the method
itself takes the number of parameters you supply.
 
Abel said:
Jon, finally I can solve the problem, but I don?t understand why.

Instead of parameters [1] = (IList)Session["AdminPaginaBase"];
I put
parameters [1] = (IList)theList;
theClass.GetMethod("fill").Invoke(o, (object[])parameters );
Session["AdminPaginaBase"] = theList;

This is the solution for this problem. But I can?t pass an Session
into a parameter?

Have you an answer?

I don't really know what you mean - but you haven't shown how you've
initialized "theList" in the above.
So, yet I can work with my 32 equals clases with one page using
reflection, inheritance and polymorphism.

Many thanks for your time

Good.
 
Back
Top