O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required