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+

Reflecting ref and out Parameter Information

Methods and their parameters can be examined using reflection to obtain detailed information about their use. When reflecting method parameters, it may be necessary to determine if they are reference or output parameters.

Parameter Reflection

In the Reflection in .NET tutorial I described the processes for reflecting a method and obtaining a MethodInfo object. This type holds lots of information about a method and permits you to retrieve further details about its parameters. You might use this information for display purposes in a programmer's tool, or to invoke the reflected methods at run time.

The tutorial article does not describe how to differentiate between standard parameters and reference or output parameters. However, it is a relatively easy process, which this article will describe.

To demonstrate, we need a method to reflect over. Create a new console application and add the following, "TestClass" class to it. The class includes a method that includes one parameter of each type.

public class TestClass
{
    public void TestMethod(int stdParam, out int outParam, ref int refParam)
    {
        outParam = -1;
    }
}

We'll add all of the code that performs the reflection to the Main method. As we will be using functionality from the System.Reflection namespace, add the following using directive to the class containing Main.

using System.Reflection;

Reflecting Parameter Details

In the earlier tutorial article I described how to reflect method parameters. First you need to obtain a Type object for the class or structure that contains the member to be reflected. You use the Type object's GetMethod method to generate a MethodInfo object, from which you can obtain the parameter details by calling GetParameters. the parameters are described in an array of ParameterInfo objects, which appear in the same order as the parameters of the method.

To demonstrate, add the following code to the Main method and run the program. This reflects the test method and outputs the names of its parameters.

Type type = typeof(TestClass);
MethodInfo info = type.GetMethod("TestMethod");
ParameterInfo[] parameters = info.GetParameters();

foreach (ParameterInfo pi in parameters)
{
    Console.WriteLine(pi.Name);
}

/* OUTPUT

stdParam
outParam
refParam

*/

Identifying ref and out Parameters

Identifying an output parameter requires only that you read a single member of the appropriate PropertyInfo object. The IsOut property returns a Boolean value that is true for output parameters and false for standard or reference parameters.

To differentiate between standard and reference parameters you need to obtain the parameter's type, by reading the ParameterType property. This returns a Type object with an IsByRef property. This returns true for output or reference parameters and false otherwise.

Using the two properties and the following table allows you to determine the type of a parameter.

Parameter TypeIsOutIsByRef
Standardfalsefalse
Outputtruetrue
Referencefalsetrue

Try running the code shown below. Here the names of the three parameters are read and displayed, along with information that highlights whether they are standard, output or reference parameters.

static void Main()
{
    Type type = typeof(TestClass);
    MethodInfo info = type.GetMethod("TestMethod");
    ParameterInfo[] parameters = info.GetParameters();

    foreach (ParameterInfo pi in parameters)
    {
        Console.WriteLine(pi.Name);
        Console.WriteLine(GetRefValueType(pi));
        Console.WriteLine();
    }
}

static string GetRefValueType(ParameterInfo pi)
{
    if (pi.IsOut)
        return "Output Parameter";
    else if (pi.ParameterType.IsByRef)
        return "Reference Parameter";
    else
        return "Standard Parameter";
}

/* OUTPUT

stdParam
Standard Parameter

outParam
Output Parameter

refParam
Reference Parameter

*/
2 January 2014