invoke methods with params


C

codefragment

Hi
I want to use reflection to call a method which accepts "params
string[]" as an argument

I've searched this group and found a better idea than invoke, just
cast to an interface and then call the method
directly, but I would still like to know if this is possible, e.g.
something like the below

ta
C




public class TestA
{
public void Meth(params string[] args)
{
}
}

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Type handlerInvoke = Type.GetType("ReflectionProject.TestA");
object targetToInvoke = Activator.CreateInstance(handlerInvoke);

MethodInfo methodToInvoke = handlerInvoke.GetMethod("Meth",
BindingFlags.Instance | BindingFlags.Public);

string[] myArgs = {"a", "b", "c"};

if (methodToInvoke != null)
{
try
{

object[] args = new object[myArgs.Length];
for (int i = 0; i < myArgs.Length; i++)
{
args = myArgs;
}
//object[] args = myArgs;

bool invokeResult = (bool)methodToInvoke.Invoke(targetToInvoke,
args);
}
catch (Exception la)
{
int a = 0; a++;
}
}
}
 
Ad

Advertisements

M

Marc Gravell

Yes it is possible, but you need to double-wrap the args - i.e. you
need:

string[] paramsArgs = {...blah...}

object[] methodArgs = {paramsArgs};

That way, it (correctly) treats it as a single argument that is an
array, rather than "n" arguments.

Marc
 
J

Jon Skeet [C# MVP]

I want to use reflection to call a method which accepts "params
string[]" as an argument

I've searched this group and found a better idea than invoke, just
cast to an interface and then call the method
directly, but I would still like to know if this is possible, e.g.
something like the below

Sort of - but you've got it slightly wrong. You're trying to invoke a
method with several string parameters, instead of with a single string
array parameter.

Try:

object[] args = new object[] { myArgs };
 
M

Marc Gravell

Like so:

using System;
using System.Reflection;

static class Program
{
static void Main()
{
MethodInfo method = typeof(Program).GetMethod("Test");
string[] values = { "foo", "bar" };
object[] args = { values };
method.Invoke(null, args);
}
public static void Test(params string[] values)
{
foreach (string val in values)
{
Console.WriteLine(val);
}
}
}
 
C

codefragment

I did try this, I get an "object reference not set" exception, i.e.

namespace ReflectionProject
{

public class TestA
{
public void Meth(params string[] args)
{
}
}

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Type handlerInvoke = Type.GetType("ReflectionProject.TestA");
object targetToInvoke = Activator.CreateInstance(handlerInvoke);

MethodInfo methodToInvoke = handlerInvoke.GetMethod("Meth",
BindingFlags.Instance | BindingFlags.Public);

string[] myArgs = {"a", "b", "c"};

if (methodToInvoke != null)
{
try
{

object[] args = { myArgs };


bool invokeResult = (bool)methodToInvoke.Invoke(targetToInvoke,
args);
}
catch (Exception la)
{
int a = 0; a++;
}
}
}
}
}
 
M

Marc Gravell

The error is because you are trying to cast the result of a "void"
method to bool. Either ignore the result, or make the method return a
bool.

Marc
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

I did try this, I get an "object reference not set" exception, i.e.

Well, it would really help if you'd give a short but complete example
which *doesn't* need to be run in ASP.NET...
 
Ad

Advertisements

C

codefragment

The error is because you are trying to cast the result of a "void"
method to bool. Either ignore the result, or make the method return a
bool.

Marc

Doh! Your quite right, thats a result of trying many things over time
and then dozily not noticing. Works now, thanks.
 

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