473,231 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 5364
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: RickDee | last post by:
Hi. I need help badly here. I need to write a program to invoke methods ( static, public, nonstatic, with parameters, and etc ) from an exe file, but I keep facing problem. From all the notes...
5
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...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
1
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, can someone please tell me why the **** this does not work as expected: First at all, thats what a WMI parameter looks like: public class WMIParameter {
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.