471,344 Members | 1,006 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

invoke methods with params

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[i] = myArgs[i];
}
//object[] args = myArgs;

bool invokeResult = (bool)methodToInvoke.Invoke(targetToInvoke,
args);
}
catch (Exception la)
{
int a = 0; a++;
}
}
}
Jun 27 '08 #1
7 5228
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
Jun 27 '08 #2
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);
}
}
}

Jun 27 '08 #3
<co**********@googlemail.comwrote:
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 };
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #4
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++;
}
}
}
}
}
Jun 27 '08 #5
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
Jun 27 '08 #6
<co**********@googlemail.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #7
On 23 Apr, 21:48, Marc Gravell <marc.grav...@gmail.comwrote:
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.

Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

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.