Parameter count mismatch error using reflection

P

puzzlecracker

I am going to post the most relavent code that causes this behavior:

Here I try to invoke a static method of TestProgram Class
-----------------------------------------------------------------------------------
string [] args={"param1","param2"};
Type t = Type.GetType("TestProgram");
MethodInfo mi = t.GetMethod("RunTest", BindingFlags.Static |
BindingFlags.Public | BindingFlags.FlattenHierarchy);
Debug.Assert(mi != null);
mi.Invoke(null, args);

--------------------------------------------------

class TestProgram {

public static void RunTest(string[] args)
{

}

}
-------------------------------------------------

mi.Invoke(null, args); raises Target ParameterException.

I appreciate your help!

Thanks
 
A

Arne Vajhøj

puzzlecracker said:
[...]
mi.Invoke(null, args); raises Target ParameterException.
Try:

mi.Invoke(null, new object[] { args });

yea, I also came to the same solution. I wonder how CLR pass this
through ... I mean, it is a hack.

It is not a hack.

The first call the method with one argument of type array of string.

The second call the method with two arguments that are both type string.

I don't think the CLR are doing much magic.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top