8.4. Invoking a Method during Runtime

The overloaded Invoke() method of the MethodInfo and ConstructorInfo classes allows us to execute the member reflected by the object of either class. To do this, we must build up an object array with the parameters to pass into the method. If it is an instance method, we must also provide an object of that type. Let's look at an example.

Our method has the following signature and return type:

static public bool
invokeMethod( string type, string method, object[] args ){}

First we retrieve a Type object of the specified type. If it cannot be found, we return false:

Type t = Type.GetType( type );
if ( t == null )
   return false;

Next we retrieve the method. Again, if we can't find it, we return false. (I can easily imagine an argument for throwing an exception instead in these two cases. We'll consider that option at the end of this section.):

MethodInfo mi = t.GetMethod( method );
if ( mi == null )
   return false;

If the method is static, we can invoke it without further work because a static method does not require an object of the class:

if ( mi.IsStatic )
     mi.Invoke( null, args );

If the method is nonstatic, however, we have to come up with an object of the type in order to invoke the method. It turns out to be very easy to do this—for example,

if ( mi.IsStatic == false ){
     object o = Activator.CreateInstance( t );
     mi.Invoke( o, args );
}

The Activator class is defined within the System namespace. It provides a set of methods to create a local or a remote object given a Type object. By default, it invokes the no-argument constructor of the associated type, if defined, to initialize the object. Alternatively, we can build up an object array of argument values to be passed to the matching constructor.

The Invoke() method returns an object holding the return value of the method, or null if the method has a void return type. Our current implementation ignores that value, and users may complain. A more appropriate implementation of our method returns the value returned by Invoke(). To indicate failure, rather than returning a false value, we now throw an exception:

static public object
invokeMethod( string type, string method, object[] args )
{
      Type t = Type.GetType( type );
      if ( t == null )
         throw Exception("Unable to find type "+type);

      MethodInfo mi = t.GetMethod( method );
      if ( mi == null )
         throw Exception("Unable to find method "+method);

      if ( mi.IsStatic )
         return mi.Invoke( null, args );

      object o = Activator.CreateInstance( t );
      return mi.Invoke( o, args );
}

In addition to the Invoke() method of the MethodInfo class, there is a GetValue() and SetValue() pair of methods in the FieldInfo class that allows us to read and write the reflected field.

Invoking the get or set accessor of a property is a two-step process:

1.
We retrieve a MethodInfo object for the get accessor through the PropertyInfo method GetGetMethod(). For the set accessor, we use GetSetMethod().

2.
We invoke the Invoke() method of the MethodInfo object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.76.204