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.

Obtaining Method Information

Earlier articles in this tutorial have described how to use reflection to examine classes and extract information about the type, its fields and its properties. In this article we will look at a similar process for obtaining method information. This includes details of the method itself and of its parameters and return type. The descriptions of the techniques are simpler than in previous articles, as they are similar to the processes for reflecting fields and for reflecting properties, each of which have already been described. If you have not already read the earlier articles you should do so before continuing.

As we will be using types from the System.Reflection namespace, add the following using directive to any code files when executing the sample code.

using System.Reflection;

We need a type with some methods to reflect over. We'll use the following test class:

public class MethodTest
{
    public static string GetMessage()
    {
        return "Hello, World!";
    }

    private int Calculate(int x, int y)
    {
        return x * y;
    }

    internal virtual void CalculateRef(int x, int y, out int result)
    {
        result  = x * y;
    }
}

MethodInfo

When retrieving details of a type's methods, the information is returned using MethodInfo objects. As when reflection of other types of member, you can retrieve details of a single method based upon its name, optionally providing binding flags. You do so using the GetMethod method.

The following sample shows the use of GetMethod with and without binding flags:

Type type = typeof(MethodTest);
MethodInfo info = type.GetMethod("GetMessage");
Console.WriteLine(info.Name);

MethodInfo info2 = type.GetMethod(
    "Calculate", BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine(info2.Name);

/* OUTPUT

GetMessage
Calculate

*/

To retrieve the details of multiple methods at the same time, use the GetMethods method. When used with no arguments, all of the public methods of the class are returned in a MethodInfo array. To reflect over non-public methods you can add binding flags.

The following sample retrieves the non-public methods from the MethodTest class, including those inherited from the object class.

Type type = typeof(MethodTest);
MethodInfo[] info = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

foreach (MethodInfo method in info)
{
    Console.WriteLine(method.Name);
}

/* OUTPUT

Calculate
CalculateRef
Finalize
MemberwiseClone

*/

MethodInfo Properties

Once you have MethodInfo instances to work with you can find out more about the referenced methods by examining their properties. As with other reflected member types, MethodInfo inherits functionality from the MemberInfo class, so has all of that type's properties. However, MethodInfo does not subclass MemberInfo directly. It is actually derived from MethodBase, which adds further properties. The MethodInfo class also defines some properties of its own.

We'll look at some of the key properties in the following sections.

MethodBase Type Properties

The MethodBase class defines six Boolean properties that can be used to obtain details about how the method was declared. They are:

  • IsAbstract. This property is true if the method is declared as abstract, meaning that it has no implementation and must be overridden by subclasses.
  • IsConstructor. This property is true if the reflected member is a constructor. When using reflection with methods, it will always return false.
  • IsFinal. The IsFinal property is true when the method is final. This means that the method cannot be overridden in a subclass. For example, when a method is sealed.
  • IsHideBySig. This property returns true when the method being reflected hides another member with the same signature. This is seen when a method has been declared using the "new" keyword in order to hide a matching member inherited from a base class.
  • IsStatic. The IsStatic property indicates whether the reflected method is a static method or an instance member. If the method is static, the property returns true.
  • IsVirtual. This property returns true if the method is virtual. This includes members that are declared with explicit use of the "virtual" keyword and members that are part of the implementation of an interface or that override an abstract method in a base class. A method being virtual does not guarantee that it can be overridden, as it may be sealed. To check if a method can be overridden, check that IsVirtual is true and IsFinal is false.
Type type = typeof(MethodTest);
MethodInfo info = type.GetMethod(
    "Calculate", BindingFlags.NonPublic | BindingFlags.Instance);

Console.WriteLine(info.IsAbstract);     // False
Console.WriteLine(info.IsConstructor);  // False
Console.WriteLine(info.IsFinal);        // False
Console.WriteLine(info.IsHideBySig);    // True
Console.WriteLine(info.IsStatic);       // False
Console.WriteLine(info.IsVirtual);      // False
9 March 2012