views:

486

answers:

2

In .net, is there a way using reflection to determine if a parameter on a method is marked with the "params" keyword?

+10  A: 

Test to see whether the final ParameterInfo has ParamArrayAttribute applied to it.

Jon Skeet
+9  A: 

Check to see if a ParamArrayAttribute has been applied to the ParameterInfo object:

static void Main()
{
    //use string.Format(str, args) as a test
    var method = typeof (string).GetMethod(
        "Format",
        new[] {typeof (string), typeof (object[])});
    var param = method.GetParameters()[1];
    Console.WriteLine(IsParams(param));
}
static bool IsParams(ParameterInfo param)
{
    return Attribute.IsDefined(param, typeof (ParamArrayAttribute));
}
Nathan Baulch
Already said, and use Attribute.IsDefined instead.
leppie