Question

Consider this method signature:

public static void WriteLine(string input, params object[] myObjects)
{
    // Do stuff.
}

How can I determine that the WriteLine method's "myObjects" pararameter uses the params keyword and can take variable arguments?

Was it helpful?

Solution

Check for the existence of [ParamArrayAttribute] on it.

The parameter with params will always be the last parameter.

OTHER TIPS

Check the ParameterInfo, if ParamArrayAttribute has been applied to it:

static bool IsParams(ParameterInfo param)
{
    return param.GetCustomAttributes(typeof (ParamArrayAttribute), false).Length > 0;
}

A slightly shorter and more readable way:

static bool IsParams(ParameterInfo param)
{
    return param.IsDefined(typeof(ParamArrayAttribute), false);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top