Information About Methods


Using the methods of Type, it is possible to obtain detailed information about a type. Once the information is available, constructors, methods and properties of the type may be viewed or called.

The following sections demonstrate four techniques:

  1. obtaining information about methods,
  2. invoking methods,
  3. constructing objects and
  4. loading types from assemblies.

Using a Type structure, it is possible to obtain information about the methods of the type using GetMethods(). One form of GetMethods() is shown below.

MethodInfo[] GetMethods()

An array of MethodInfo structures is returned. Each MethodInfo structure represents an individual method of the class. The class MethodInfo may be found in the namespace System.Reflection.

The class MethodInfo is derived from the abstract base class MethodBase which inherits MemberInfo. The properties and methods defined by all three of these classes are found in MethodInfo. For example, to obtain the name of the method, the property Name is used. Two other members include ReturnType and GetParameters(). The return type of a method is found in the readonly property ReturnType. ReturnType returns an object of type Type. The method GetParameters() return a list of parameters for the method. It has the following form.

ParameterInfo[] GetParameters();

The information about each parameter is stored in an object of the type ParameterInfo. The type ParameterInfo defines a large number of properties and methods that describe a parameter. Two properties that will be used are Name, which is a string containing the name of the parameter and ParameterType, which describes the parameter's type. The type of the parameter is represented by an object of Type.

Below is a program that reconstructs a description of the methods of a class using reflection. The above mentioned properties and methods are used.

// Runtime5 - Querying Methods

using System;
using System.Reflection;

class X
{
    public int x;
    public int y;

    public X(int i, int j) { x = i; y = j; }

    public int Sum() { return x + y; }
        
    public void Set(int i, int j)
    {
        x = i; y = j; 
        Console.WriteLine("In Set(int,int), value={0}", this);
    }

    public void Set(double u, double v)
    {
        x = (int)u; y = (int)v;
        Console.WriteLine("In Set(double,double, value={0})", this);
    }

    public override string ToString()
    {
        return "(" + x + "," + y + ")";
    }
}

class Program
{
    static void Main()
    {
        Type t = typeof(X);

        Console.WriteLine("Analysing methods in type {0}", t.Name);

        MethodInfo[] methods = t.GetMethods();

        foreach (MethodInfo mi in methods)
        {
            Console.Write(mi.ReturnType.Name + " " + mi.Name + "(");
            
            ParameterInfo[] parameters = mi.GetParameters();

            for (int i = 0; i < parameters.Length; i++)
            {
                Console.Write(parameters[i].ParameterType.Name + " " + parameters[i].Name);
                if (i + 1 < parameters.Length) Console.Write(",");
            }

            Console.WriteLine(")");
            Console.WriteLine();
        }
    }
}

The output of this program is shown below.

Analysing methods in type X
Int32 Sum()

Void Set(Int32 i,Int32 j)

Void Set(Double u,Double v)

String ToString()

Type GetType()

Boolean Equals(Object obj)

Int32 GetHashCode()

The methods from the base class object as well as from X are enumerated. GetMethods() is called to obtain an array of MethodInfo structures. The property ReturnType (of MethodInfo) is used to describe the return type of the method. The method GetParameters() of MethodInfo is called to obtain information about the parameters of each method. By doing this, the form of the method is reconstructed and printed.

A Second Form of GetMethods()

A second form of GetMethods() allows various flags to be specified. These flags filter the methods that are retrieved. This second form is shown below.

MethodInfo[] = GetMethods(BindingFlags flags)

The BindingFlags are shown in the table below.

DeclaredOnly Retrieves only the methods defined by the class. Inherited methods are not included.
Instance Retrieves instance methods.
NonPublic Retrieves nonpublic methods.
Public Retrieves public methods.
Static Retrieves static methods.

The flags in this table can be combined using the or operator.

Exercise

Update the previous program to include only the methods of the class - not the base class methods.