473,480 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error trying to execute reflection "invoke" method

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
Nov 15 '05 #1
10 2151
maybe this will help

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

--
/*
Vladimir Scherbina,
Ukraine, Kiev.
*/
"Abel" <an*******@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
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

Nov 15 '05 #2
Vladimir S. <vl*********@ukr.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
try casting the "o" value....It might solve the problem

--
Rami Saad
Microsoft GTSC Developer support for Middle East
"Abel" <an*******@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
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

Nov 15 '05 #4
Rami Saad <ra*******@egdsc.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
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

Nov 15 '05 #6
Abel <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
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
Nov 15 '05 #8
Abel <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Abel <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Abel <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
401
by: Walter Zydhek | last post by:
Can anyone help with the use of the Invoke method of a Directory Entry object? I am using vb.net. I am trying to invoke the get_Filter method of the IIS://LocalHost directory entry, and get the...
0
4367
by: PJ | last post by:
Hello, I am trying to use late binding to call a COM object. I am trying to call a 'GetTables' method on the object. It's essentially a 'MetadataService' which is used to return the names of...
0
1817
by: Pedro CLIKEAR | last post by:
The problem happens when we try to set the password for two different users with DirectoryEntry.Invoke method. The first time we use user.Invoke("SetPassword",new object {pwdUsuario}); it works...
5
4278
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
11
1732
by: S Shulman | last post by:
Hi I am looking for a method that allows calling a method that will be executed by the main application thread bu will be called from any thread that I create manually. (I think that...
0
922
by: Sundar.k | last post by:
iam invoking a com through reflection i need to pass the value by reference how do i do it i used parameter modifier bu i guess i messed it
3
7101
by: SatCom | last post by:
Hello, I had originally posted this in the winforms.controls discussion, forgive the double post, Here is where I need help... I have been porting some VB6 to VB2005 and here is the issue with...
3
5241
by: =?Utf-8?B?Sm9l?= | last post by:
I know that I have posted this question before, but it is still unresolved and I don't know where to turn to next. I have code that is creating a user (works fine), then sets the account flags...
6
39420
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.