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.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Type handlerInvoke = Type.GetType("R eflectionProjec t.TestA");
object targetToInvoke = Activator.Creat eInstance(handl erInvoke);
MethodInfo methodToInvoke = handlerInvoke.G etMethod("Meth" ,
BindingFlags.In stance | BindingFlags.Pu blic);
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)methodToI nvoke.Invoke(ta rgetToInvoke,
args);
}
catch (Exception la)
{
int a = 0; a++;
}
}
}
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.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Type handlerInvoke = Type.GetType("R eflectionProjec t.TestA");
object targetToInvoke = Activator.Creat eInstance(handl erInvoke);
MethodInfo methodToInvoke = handlerInvoke.G etMethod("Meth" ,
BindingFlags.In stance | BindingFlags.Pu blic);
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)methodToI nvoke.Invoke(ta rgetToInvoke,
args);
}
catch (Exception la)
{
int a = 0; a++;
}
}
}
Comment