How to use introspection to call method dynamically?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Let's say I have the following class:

class ccTestClass
{
public void doMethod()
{
}
}

If I have the name of the method stored in some variable x, such that String
x = "doMethod", how can I use introspection to call doMethod() dynamically
from within the code.

Thanks.

Mansi
 
Hi Mansi:

Yes, you can use classes System.Reflection:

Type t = typeof(ccTestClass);

ccTestClass test = new ccTestClass();
string method = "doMethod";
Binder binder = null;
object[] invokeArgs = null;

t.InvokeMember(
method,
BindingFlags.InvokeMethod,
binder,
test,
args
);

HTH,
 
Back
Top