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++;
}
}
}
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++;
}
}
}