Invoking a Method Dynamically


In the same way that you can set or get the value of a field, you can also invoke a method dynamically. Invoking a method dynamically is done with a MethodInfo object.

To invoke a method dynamically at runtime:

  1. Assuming that you have an object of type System.Type in a variable called currtype , type: System.Reflection.MethodInfo mi = currtype.GetMethod("methodname"); where fieldname is the name of the field in the class you wish to set or get.

  2. Type object[] params = new object[] {"val1" , val2 , val3} where val1 , val2 , and val3 are the variables or literals representing the parameters for the method.

  3. If the method is static, type object result = mi.Invoke(null,params); If you know the return type, use the specific type when declaring the result variable. For example, instead of object result, type int result = (int) mi.Invoke(null,params) .

    or

    If the method is an instance method, type mi.Invoke(obj, params) , where obj is an instance of a class containing the method ( Figure 12.20 ).

    Figure 12.20 Invoke returns an object with the return value of the function. You can then cast the object to the type the function returns.
     //this is the definition for the method //we want to invoke //public bool SendLetter(string letter) MethodInfo mi = typeAddress.GetMethod("SendLetter"); //the second parameter is an array of //parameters bool result = (bool)mi.  Invoke  (        objAddress,        new object[] {"Dear Mr. Rogers"}); 

graphics/tick.gif Tips

  • The preceding steps enable you to locate a method within the class using the method name. It is possible that the class has overloaded methods , and therefore has more than one method with the same name. However, overloading is legal only if you change the casing of the method, add or remove parameters from the parameter list, or change the type of some of the parameters. You can call a version of GetMethod() that lets you specify the parameters types for the method to use in cases where overloading is a possibility ( Figure 12.21 ).

    Figure 12.21 For you to have overloaded functions, something needs to be different in each function's parameter list. GetMethod lets you pass in an array of types. The function will then match the types and the number of elements to the types and number of arguments in one of the overloaded functions.
     //there are two definitions of the //Demolish method //public void Demolish() //public void Demolish(string reason) //the second parameter is an array of //types that the function uses to find //which version of Demolish to use MethodInfo methodDem = typeAddress.GetMethod(            "Demolish",  new Type[] {typeof(string)}  ); methodDem.Invoke(objAddress,            new object[] {"Highway"}); 
  • With enough security you can invoke a private method in a class (see Code Access Security in the MSDN documentation for details).




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net