Calling a function from a string in C#

Jeremy Boyd picture Jeremy Boyd · Feb 12, 2009 · Viewed 200.4k times · Source

I know in php you are able to make a call like:

$function_name = 'hello';
$function_name();

function hello() { echo 'hello'; }

Is this possible in .Net?

Answer

ottobar picture ottobar · Feb 12, 2009

Yes. You can use reflection. Something like this:

Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);