Using reflection on a method definition I would like to find out if the original method was defined with 'params' on the last parameter. So can I discover if the original definition was this...
public void MyMethod(int x, params object[] args);
...and not this...
public void MyMethod(int x, object[] args);
My code has a list of arguments and is using reflection to call an arbitrary method. If it is marked with 'params' then I want to package up the extra parameters into an object[] and call the method. If the argument is not marked with 'params' then I would indicate an error instead. So I want to provide the same semantics as C#.
But I cannot find any docs that indicate how to discover this using reflection.
Use ParamArrayAttribute attribute
class Program
{
public void MethodWithParams(object param1, params int[] param2)
{
}
static void Main(string[] args)
{
var method = typeof(Program).GetMethod("MethodWithParams");
var @params = method.GetParameters();
foreach (var param in @params)
{
Console.WriteLine(param.IsDefined(typeof(ParamArrayAttribute), false));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With