BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Detecting a Parameter Array

Reflection permits .NET developers to interrogate type metadata and obtain information relating to items such as classes, structures and their members. When reflecting methods it may be necessary to check if the final argument is a parameter array.

Parameter Arrays

If the final parameter of a method accepts an array, you can declare it as a parameter array by prefixing it with the params keyword. When using such an argument you can pass it either an array or a series of comma-separated values of the correct type. Using the latter option allows you to create methods that seemingly accept any number of arguments. Within the method the comma-separated list is transparent; it becomes a simple array.

If you are using reflection to examine a method and its parameters, you may wish to know whether a parameter array argument is allowed. This article explains how to detect this.

For the example code we need a sample type to reflect over. We'll use the class below, which contains two similar methods. The first includes a parameter array, which accepts either an array or a list of values. The second requires that an array is provided. The methods have no content, as this is not required for our purposes.

public class Test
{
    public void WithParams(params string[] someStrings)
    {
    }

    public void WithoutParams(string[] someStrings)
    {
    }
}

Checking for a Parameter Array

As you might expect, to detect a parameter array you first need to obtain a ParameterInfo object that represents the parameter being examined. We'll use several steps to get such an object. First we'll obtain a Type instance for the Test class. Once we have this we can call GetMethod in order to acquire a MethodInfo object for each of the WithParams and WithoutParams methods.

We'll use the MethodInfo's GetParameters method to retrieve ParameterInfo objects for each parameter. In our case we will extract the first item from the returned array, as we know we only have one argument in each method. When you have multiple ParameterInfo objects in the array you should check the last one, as the params keyword may only be applied to the final parameter.

For example, to obtain a ParameterInfo object for the someStrings parameter of the WithParams method we can use the following code. NB: The sample code assumes that you have included a using directive that references the System.Reflection namespace.

Type type = typeof(Test);
MethodInfo withInfo = type.GetMethod("WithParams");
ParameterInfo withParameter = withInfo.GetParameters()[0];

ParameterInfo does not include a property that instantly tells you if the reflected parameter is a parameter array. Instead, we need to check whether it has been decorated with the ParamArray attribute. We can determine this using the GetCustomAttribute method, passing the type of the attribute in question. If the attribute is defined, we'll be given a reference to it. If not, the return value will be null.

The sample code below puts all of this together. It reflects the parameters of both test methods. The console output shows that the parameter array is correctly detected.

Type type = typeof(Test);

MethodInfo withInfo = type.GetMethod("WithParams");
ParameterInfo withParameter = withInfo.GetParameters()[0];
Attribute withAttr = withParameter.GetCustomAttribute(typeof(ParamArrayAttribute));
bool with = withAttr != null;

MethodInfo withoutInfo = type.GetMethod("WithoutParams");
ParameterInfo withoutParameter = withoutInfo.GetParameters()[0];
Attribute withoutAttr = withoutParameter.GetCustomAttribute(typeof(ParamArrayAttribute));
bool without = withoutAttr != null;

Console.WriteLine("With: {0}", with);
Console.WriteLine("Without: {0}", without);

/* OUTPUT

With: True
Without: False

*/
21 December 2012