我如何可以调用使用反射一个可变参数的方法?像这样的:调用可变参数方法
public static void ArgsMethod(__arglist)
{
ArgIterator ai = new ArgIterator(__arglist);
while(ai.GetRemainingCount() > 0)
{
Console.WriteLine(TypedReference.ToObject(ai.GetNextArg()));
}
}
typeof(Program).GetMethod("ArgsMethod").Invoke(null,new object[0])
抛出NotSupportedException异常。
我怀疑你需要'TypedReference'。 – SLaks 2014-12-05 16:05:36
调用传递方法名称和方法参数类型数组的GetMethod。对于可变参数方法,类型是一个Object数组。 – 2014-12-05 16:06:38
@Max 1)'GetMethod(字符串,类型[])'仅检查固定的参数类型,它不创建基于可变参数类型的方法。 2)它宁愿是一个空的Type数组,因为我不会传递任何对象给方法。 – IllidanS4 2014-12-05 16:14:31