invoke a method by reflection£¬the method's parameters can not be ArrayList?

  • Thread starter Thread starter jerry051
  • Start date Start date
J

jerry051

I invoke a method by reflection, when this method's parameters is simple
type like int or string,the invoking is correct and secceed. But when the
parameters is ArrayList type, debuger tips me that meet
System.Reflection.TargetParameterCountException error, and parameters count
is not matching. please help me why happen this error,thanks a lot!
 
jerry051 said:
I invoke a method by reflection, when this method's parameters is simple
type like int or string,the invoking is correct and secceed. But when the
parameters is ArrayList type, debuger tips me that meet
System.Reflection.TargetParameterCountException error, and parameters count
is not matching. please help me why happen this error,thanks a lot!

Please show us the code you are using when you invoke the method.


Oliver Sturm
 
Hi,

Show the code, you should use the very same approach , create an object[]
and construct it with your parameters. it should work fine


cheers,
 
jerry051 wrote:

Okay. The reason for your problem lies in these two lines:
public object Exec(ArrayList parameters)

The Exec method is declared to receive one parameter: An ArrayList.
object temp = myType.GetMethod("Exec").Invoke(myInstance, new
object[]{"a","b","c","d","e"}); //this will cause exception that i
describing ahead!

But your call passes five parameters, not one. This doesn't fit,
obviously. Maybe you were expecting some automagical conversion to take
place, well, bad luck in that case :-)

You'll have to do one of two things. You can (a) create an arraylist to
pass to your method:

ArrayList list = new ArrayList(new string[] {
"a","b","c","d","e" });
object temp = myType.GetMethod("Exec").Invoke(myInstance,
new object[]{ list });

Or your can change your Exec method to take a list of parameters instead
of the arraylist:

public object Exec(params object[] parameters) {
...


Oliver Sturm
 
Oliver Sturm: hi,thank u very much ,i have dealed this proplem :)
you are right, it's my inattention with parameters pass.


jerry051 said:
I accept your advise, changed my method parameters ,but it still report
same error! could you help me to read source code (accessory ) look for
where is incorrect?

Oliver Sturm said:
jerry051 wrote:

Okay. The reason for your problem lies in these two lines:
public object Exec(ArrayList parameters)

The Exec method is declared to receive one parameter: An ArrayList.
object temp = myType.GetMethod("Exec").Invoke(myInstance, new
object[]{"a","b","c","d","e"}); //this will cause exception that i
describing ahead!

But your call passes five parameters, not one. This doesn't fit,
obviously. Maybe you were expecting some automagical conversion to take
place, well, bad luck in that case :-)

You'll have to do one of two things. You can (a) create an arraylist to
pass to your method:

ArrayList list = new ArrayList(new string[] {
"a","b","c","d","e" });
object temp = myType.GetMethod("Exec").Invoke(myInstance,
new object[]{ list });

Or your can change your Exec method to take a list of parameters instead
of the arraylist:

public object Exec(params object[] parameters) {
...


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
Back
Top