我知道在 php 中你可以进行如下调用:
$function_name = 'hello';
$function_name();
function hello() { echo 'hello'; }
这在.Net 中可能吗?
是的。您可以使用反射。像这样的事情:
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);
对于上面的代码,调用的方法必须具有访问修饰符public
。如果调用非公共方法,则需要使用BindingFlags
参数,例如BindingFlags.NonPublic | BindingFlags.Instance
:
Type thisType = this.GetType();
MethodInfo theMethod = thisType
.GetMethod(TheCommandString, BindingFlags.NonPublic | BindingFlags.Instance);
theMethod.Invoke(this, userParameters);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)