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 Method Information

The fifth part of the Reflection tutorial describes the reflection of methods. The System.Type class includes several methods that return MethodInfo objects that describe a class' methods. These can be used to obtain parameter and return value details.

Scope Properties

The MethodBase class also defines six properties that allow you to determine the scope of the reflected member. These are:

  • IsAssembly. If true, the method is visible to all classes within its containing assembly and any friend assemblies. In C#, these are internal fields.
  • IsFamily. If true, the method is visible within the containing class and its subclasses. These are protected fields.
  • IsFamilyAndAssembly. If true, the method is visible within its containing class and any subclasses within the same assembly. These are protected private fields in C++.
  • IsFamilyOrAssembly. This property is true for methods that are visible within the containing class, any subclasses and any other class in the same assembly or a friend assembly. These methods have the protected internal scope.
  • IsPrivate. True for private methods that are visible only within their own class.
  • IsPublic. If true, the method is public, meaning that it is visible to any class in any assembly.
Type type = typeof(MethodTest);
MethodInfo info = type.GetMethod(
    "Calculate", BindingFlags.NonPublic | BindingFlags.Instance);

Console.WriteLine(info.IsAssembly);          // False
Console.WriteLine(info.IsFamily);            // False
Console.WriteLine(info.IsFamilyAndAssembly); // False
Console.WriteLine(info.IsFamilyOrAssembly);  // False
Console.WriteLine(info.IsPrivate);           // True
Console.WriteLine(info.IsPublic);            // False

MethodInfo Properties

Finally, let's look at the key properties of the MethodInfo class itself:

  • ReturnParameter. Returns information about the return value of the method as a ParameterInfo object. We'll see some ParameterInfo class properties in the next section.
  • ReturnType. Returns information about the type of the return value as a System.Type object.
Type type = typeof(MethodTest);
MethodInfo info = type.GetMethod(
    "Calculate", BindingFlags.NonPublic | BindingFlags.Instance);

Console.WriteLine(info.ReturnParameter);     // Int32
Console.WriteLine(info.ReturnType.FullName); // System.Int32

Obtaining Method Parameter Information

To interrogate the parameters of a method you need to obtain ParameterInfo objects. You can do this using the GetParameters method of the MethodBase class, a member that returns a ParameterInfo array with one element representing each argument.

The following sample obtains the parameters of the CalculateRef method:

Type type = typeof(MethodTest);
MethodInfo info = type.GetMethod(
    "CalculateRef", BindingFlags.NonPublic | BindingFlags.Instance);
ParameterInfo[] parameters = info.GetParameters();

ParameterInfo Properties

The ParameterInfo class includes a number of properties that describe a parameter. Three commonly used examples are:

  • Name. Returns the name of the parameter.
  • ParameterType. Returns the parameter type as a System.Type instance that may be interrogated further.
  • Position. Returns a zero-based index for the parameter.
Type type = typeof(MethodTest);
MethodInfo info = type.GetMethod(
    "CalculateRef", BindingFlags.NonPublic | BindingFlags.Instance);
ParameterInfo paramInfo = info.GetParameters()[2];

Console.WriteLine(paramInfo.Name);          // result
Console.WriteLine(paramInfo.ParameterType); // Int32&
Console.WriteLine(paramInfo.Position);      // 2
9 March 2012